升级SQLite数据库时出错

时间:2011-02-12 14:47:23

标签: android sqlite

我正在尝试为我的Android应用程序实现数据库升级。我尝试使用以下代码:

// Create a table that is equivalent to the existing one but with one column as TEXT instead of INTEGER
db.execSQL("CREATE VIRTUAL TABLE distributors_new USING FTS3 (fname TEXT, lname TEXT, phoneNumber TEXT, email TEXT, notes TEXT, parentid INTEGER, customid TEXT FOREIGN KEY (parentid) REFERENCES distributors(rowid) ON DELETE CASCADE ON UPDATE CASCADE);");
//Insert all data from the old table to the new one.
db.execSQL("INSERT INTO distributors_new SELECT * FROM distributors;");
// Drop the old table
db.execSQL("DROP TABLE distributors;");
// Rename the new table
db.execSQL("ALTER TABLE distributors_new RENAME TO distributors;");

但是在执行onUpgrade方法后出现错误。

02-12 16:31:16.324: ERROR/Database(1456): Failure 1 (SQL logic error or missing database) on 0x26b338 when executing 'COMMIT;'
02-12 16:31:16.334: ERROR/SQLiteOpenHelper(1456): Couldn't open mlmDB for writing (will try read-only):
02-12 16:31:16.334: ERROR/SQLiteOpenHelper(1456): android.database.sqlite.SQLiteException: SQL logic error or missing database: COMMIT;
02-12 16:31:16.334: ERROR/SQLiteOpenHelper(1456)

我做错了什么?

4 个答案:

答案 0 :(得分:1)

我不是SQL的专家,但在我看来,你没有连接到数据库服务器,或者你没有你认为的特权级别。

答案 1 :(得分:1)

我添加了

db.beginTransaction();
// Code
db.endTransaction();

现在它似乎有效。

答案 2 :(得分:1)

有,AFAIK,没有理由这样做。 Sqlite使用dynamic typing,因此创建表时的数据类型并不重要。它只影响类型亲和力,请检查链接文档。

答案 3 :(得分:0)

失败可能是由于'将'置'到非NULL列'。

Source

相关问题