0

我目前正在为 android 应用程序使用 sqlite 数据库。

首先,我想知道是否有适当的方法可以根据新信息升级 sqlite 数据库。目前,我将我的信息存储在一个文本文件中,第一行是数据库的行数。我读了第一行,看看数据库中有多少行。如果文本文件中有更多行,那么我升级数据库并将所有内容再次添加到数据库中。

这是我目前的代码:

try {
        InputStream is = getResources().getAssets().open("Questions");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        AssistedMemDB db = new AssistedMemDB(this);
        db.open();
        int tempNum = db.getNumber();
        String line = "";
        line = reader.readLine();
        if (Integer.decode(line) > tempNum) {
            db.close();
            db.upgrade();
            db.open();
            while ((line = reader.readLine()) != "") {
                String list[] = line.split("\\<>");
                db.createEntry(list[0], list[1]);
            }
        }
        db.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

我收到一条错误消息:

java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:SQLiteDatabase

先感谢您!

4

1 回答 1

0

You don't upgrade a database to add new rows to it. You upgrade a database when you have new tables or new columns to add. When adding rows, you just do an insert and put the new data in with the old data.

Also, the db helper automatically takes care of calling onUpgrade for you. All you have to do it increment the db version when you want to change the schema.

于 2013-03-30T05:03:09.597 回答