【问题标题】:SQLite keep expandingSQLite 不断扩展
【发布时间】:2012-06-13 02:13:16
【问题描述】:

我是黑莓开发和这个网站的新手。现在,我正在开发一个从 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();
    }
}

【问题讨论】:

  • 这两个函数看起来就像每次调用它们时都会添加来自 JSON 源的所有信息。你怎么称呼他们?你如何防止它们被运行两次?功能本身是否应该拒绝重新添加内容?还是应该仅在数据库不存在时才调用函数?如果将它们拆分为创建表的函数、填充表的函数和创建数据库句柄的函数,这可能更容易阅读。每个函数的目标是 15 行或更少的代码......
  • 萨诺德,感谢您的快速回复。我会努力的。我会将代码拆分为方法。但是,我问的是同样的问题,我怎样才能防止方法或代码运行两次。
  • 非常感谢萨诺德。我试过你给我的代码。我在构造函数中调用了这两个方法,现在数据库甚至没有显示。
  • 人力资源管理;你能在调试器中运行你的代码,让你逐行通过你的代码吗?这可能是最简单的下一步。

标签: sqlite blackberry


【解决方案1】:

这取决于您的目标是什么。如果你想在每次运行 json 查询时替换数据库中的数据,你应该添加一个 sqlite 命令来删除所有现有的行,并使用通过 JSON 传入的新获取的行。

如果你只是想让某些类型的记录保持唯一,你应该给 sqlite 表添加一个索引。 'ID' 列是可能的候选者。您必须做一些实验以确保正确处理冲突 - 它可能会中止整个事务。 “插入或替换”在这种情况下很有用。

【讨论】:

  • 谢谢迈克尔。实际上,在我的应用程序中,我需要在第一次打开应用程序时将 json 保存在数据库中。之后,我将需要放置一个手动刷新数据库的代码。用户应该能够选择一个特定的表并单击刷新以更新该表。现在,我正在将代码分成更小的方法,我希望这会有所帮助。顺便说一句,我试图为你的答案投票,但我还是新手,所以我不能抱歉。我会在未来:)。
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
相关资源
最近更新 更多