1

ブラックベリーの開発とこのサイトは初めてです。現在、json サービスからデータを取得するアプリに取り組んでいます。私のアプリでは、データをデータベースに解析し、4 つのテーブルに保存する必要があります。私はすでにデータを解析しており、データベースを作成して最初と 2 番目のテーブルを追加することに成功しました。

私が今直面している問題は、データベースの 2 番目のテーブルが拡大し続けることです。SQLブラウザでデータベースをチェックしたところ、アプリのアイコンをクリックするたびに、テーブルに700行が追加されることがわかりました(例:700が1400になります)。

(2番目のテーブルに対してのみ、最初のテーブルはうまく機能します)。

前もって感謝します

これは私のコードです:

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

それはあなたの目標がここにあるかによって異なります。json クエリが実行されるたびにデータベース内のデータを置き換えたい場合は、SQLite コマンドを追加して、既存のすべての行を削除し、JSON 経由で新しくフェッチされた行を含める必要があります。

特定のタイプのレコードを一意に保ちたい場合は、sqlite テーブルにインデックスを追加する必要があります。「ID」列は、この可能性が高い候補です。競合が正しく処理されることを確認するために、いくつかの実験を行う必要があります。トランザクション全体が中止される可能性があります。そんなときに役立つのが「INSERT OR REPLACE」です。

于 2012-06-13T17:35:02.263 に答える