我是新來的黑莓發展和本網站。現在,我正在研究從json服務中檢索數據的應用程序。在我的應用程序中,我應該將數據解析爲數據庫並將其保存在四個表中。我已經解析了數據,並且我成功地創建了數據庫並添加了第一個和第二個表。SQLite不斷擴大
我現在面臨的問題是,我的數據庫中的第二張表不斷擴大。我在sql瀏覽器中檢查了數據庫,發現每次點擊應用程序圖標時,它都會再次向表中添加700行(例如,700會變成1400)。
(只有第二個表,第一個表工作得很好)。
預先感謝您
這是我的代碼:
public void parseJSONResponceInBB(String jsonInStrFormat)
{
try {
JSONObject json = newJSONObject(jsonInStrFormat);
JSONArray jArray = json.getJSONArray("tables");
for (inti = 0; i < jArray.length(); i++) {
//Iterate through json array
JSONObject j = jArray.getJSONObject(i);
if (j.has("Managers")) {
add(new LabelField("Managers has been added to the database"));
JSONArray j2 = j.getJSONArray("Managers");
for (intk = 0; k < j2.length(); ++k) {
JSONObject MangersDetails = j2.getJSONObject(k);
if (MangersDetails.has("fName")) {
try {
URI myURI =
URI.create
("file:///SDCard/Databases/SQLite_Guide/"
+ "MyTestDatabase.db");
d = DatabaseFactory.openOrCreate(myURI);
Statement st =
d.createStatement
("CREATE TABLE Managers ("
+ "fName TEXT, " +
"lName TEXT, " + "ID TEXT," + "Type TEXT)");
st.prepare();
st.execute();
st.close();
d.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
try {
URI myURI =
URI.create
("file:///SDCard/Databases/SQLite_Guide/"
+ "MyTestDatabase.db");
d = DatabaseFactory.open(myURI);
Statement st =
d.createStatement
("INSERT INTO Managers(fName, lName, ID, Type) "
+ "VALUES (?,?,?,?)");
st.prepare();
for (intx = 0; x < j2.length(); x++) {
JSONObject F = j2.getJSONObject(x);
//add(new LabelField ("f"));
st.bind(1, F.getString("fName"));
st.bind(2, F.getString("lName"));
st.bind(3, F.getString("ID"));
st.bind(4, F.getString("Type"));
st.execute();
st.reset();
}
d.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
}
}
}
catch(JSONException e) {
e.printStackTrace();
}
}
//Owners method
public voidparseJSONResponceInBB1(String jsonInStrFormat)
{
try {
JSONObject json = newJSONObject(jsonInStrFormat);
JSONArray jArray = json.getJSONArray("tables");
for (inti = 0; i < jArray.length(); i++) {
//Iterate through json array
JSONObject j = jArray.getJSONObject(i);
if (j.has("Owners")) {
add(new LabelField("Owners has been added to the database"));
JSONArray j2 = j.getJSONArray("Owners");
for (intk = 0; k < j2.length(); ++k) {
JSONObject OwnersDetails = j2.getJSONObject(k);
if (OwnersDetails.has("fName")) {
try {
Statement st =
d.createStatement
("CREATE TABLE Owners ("
+ "fName TEXT, " +
"lName TEXT, " + "ID TEXT," + "Type TEXT)");
st.prepare();
st.execute();
st.close();
d.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
try {
Statement st =
d.createStatement
("INSERT INTO Owners(fName, lName, ID, Type) "
+ "VALUES (?,?,?,?)");
st.prepare();
for (intx = 0; x < j2.length(); x++) {
JSONObject F = j2.getJSONObject(x);
//add(new LabelField ("f"));
st.bind(1, F.getString("fName"));
st.bind(2, F.getString("lName"));
st.bind(3, F.getString("ID"));
st.bind(4, F.getString("Type"));
st.execute();
st.reset();
}
d.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
}
}
}
catch(JSONException e) {
e.printStackTrace();
}
}
這兩個函數看起來就像他們將會一樣,每次調用時都會從JSON源中添加所有信息。你打電話給他們怎麼樣?你怎麼阻止他們跑兩次?這些功能本身是否應該拒絕重新添加內容?還是應該只在數據庫不存在的情況下調用函數?如果將這些拆分爲創建表的函數,填充表的函數和創建數據庫句柄的函數,可能會更容易閱讀。針對每個功能的十五行代碼或更少... – sarnold
Sarnold,感謝您的快速響應。我會爲此努力。我將把代碼分解成方法。但是,我在問同樣的問題,我該如何防止方法或代碼運行兩次。 –
非常感謝Sarnold。我嘗試了你爲我提供的代碼。我在構造函數中調用了這兩個方法,現在甚至沒有顯示數據庫。 –