1

我是黑莓开发和这个网站的新手。现在,我正在开发一个从 json 服务中检索数据的应用程序。在我的应用程序中,我应该将数据解析到数据库中并将其保存在四个表中。我已经解析了数据,并且成功地创建了数据库并添加了第一个和第二个表。

我现在面临的问题是,我数据库中的第二个表不断扩大。我在 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();
    }
}
4

1 回答 1

1

It depends on what your goals are here. If you want to replace the data in the database each time the json query runs, you should add a sqlite command to remove all the existing rows with the newly fetched ones coming in via JSON.

If you just want to keep certain types of records unique, you should add an index to the sqlite table. The 'ID' column is a likely candidate for this. You'll have to do some experiments to make sure a conflict is handled correctly - it may abort the entire transaction. "INSERT OR REPLACE" is useful in that situation.

于 2012-06-13T17:35:02.263 回答