简体   繁体   English

Android SQLite OnUpgrade错误

[英]Android SQLite OnUpgrade Error

I'm currently using an sqlite database for an android app. 我目前正在将sqlite数据库用于android应用。

Firstly I am wondering if there is a proper way to upgrade a sqlite database based on new information. 首先,我想知道是否存在基于新信息升级sqlite数据库的正确方法。 Currently I have my information stored within a text file with the number of lines for the database as the first line. 目前,我的信息存储在文本文件中,数据库的行数作为第一行。 I read the first line, see how many lines there are in the database. 我读了第一行,看看数据库中有多少行。 If there are more lines in the text file then I upgrade the database and add everything into the database again. 如果文本文件中还有更多行,那么我将升级数据库,然后将所有内容再次添加到数据库中。

This is my code currently: 这是我目前的代码:

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();
    }

I'm getting an error saying: 我收到一个错误消息:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:SQLiteDatabase

Thank you in advance! 先感谢您!

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. 另外,数据库帮助程序会自动为您调用onUpgrade。 All you have to do it increment the db version when you want to change the schema. 当您要更改架构时,您要做的所有事情都会增加数据库版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM