2012-06-13 26 views
1

我是新來的黑莓發展和本網站。現在,我正在研究從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(); 
    } 
} 
+1

這兩個函數看起來就像他們將會一樣,每次調用時都會從JSON源中添加所有信息。你打電話給他們怎麼樣?你怎麼阻止他們跑兩次?這些功能本身是否應該拒絕重新添加內容?還是應該只在數據庫不存在的情況下調用函數?如果將這些拆分爲創建表的函數,填充表的函數和創建數據庫句柄的函數,可能會更容易閱讀。針對每個功能的十五行代碼或更少... – sarnold

+0

Sarnold,感謝您的快速響應。我會爲此努力。我將把代碼分解成方法。但是,我在問同樣的問題,我該如何防止方法或代碼運行兩次。 –

+0

非常感謝Sarnold。我嘗試了你爲我提供的代碼。我在構造函數中調用了這兩個方法,現在甚至沒有顯示數據庫。 –

回答

1

這取決於你的目標是在這裏。如果要在每次運行json查詢時替換數據庫中的數據,則應該添加一個sqlite命令以刪除所有現有行,並使用通過JSON進入的新獲取行。

如果你只是想保持某些類型的記錄是唯一的,你應該添加一個索引到sqlite表。 'ID'列可能是這個候選人。你必須做一些實驗來確保正確處理衝突 - 它可能會中止整個事務。 「插入或替換」在這種情況下很有用。

+0

謝謝邁克爾。實際上,在我的應用程序中,我需要在第一次打開應用程序時將json保存在數據庫中。之後,我需要放置一個手動刷新數據庫的代碼。用戶應該能夠選擇一個特定的表並點擊刷新來更新該表。現在,我正在將代碼分成更小的方法,我希望這會有所幫助。順便說一句,我試圖爲您的答案投票,但我仍然是新的,所以我不能抱歉。我將在未來:)。 –