简体   繁体   English

升级SQLite数据库时出错

[英]Error when upgrading SQLite database

I am trying to implement database upgrading for my android app. 我正在尝试为我的Android应用程序实现数据库升级。 I tried to use following code: 我尝试使用以下代码:

// 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;");

However I got an error after the onUpgrade method is executed. 但是执行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)

What am I doing wrong? 我究竟做错了什么?

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

I added 我加了

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

And now it seems to work. 现在,它似乎起作用了。

There is, AFAIK, no reason to do this. AFAIK没有理由这样做。 Sqlite uses dynamic typing , so the data type when you create the table has little importance. Sqlite使用动态类型化 ,因此创建表时的数据类型重要性不大。 It only affects the type affinity, check the linked documentation. 它仅影响类型关联,请检查链接的文档。

The failure may be due to 'putting 'NULL' into non NULL column '. 失败可能是由于“将'NULL'放入非NULL列'。

Source 资源

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

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