Closed wjbd closed 7 years ago
【Failed to restore data from temp table 】USER_TEMP android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: USER.PRICE (code 1299)
经测试,只要有数据库有数据,添加int类型数据,就会报错 【Failed to restore data from temp table 】USER_TEMP android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: USER.CDNAME (code 1299) at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method) at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734) at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754) at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1679) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608) at org.greenrobot.greendao.database.StandardDatabase.execSQL(StandardDatabase.java:37) at com.example.greendao.dbutils.MigrationHelper.restoreData(MigrationHelper.java:177) at com.example.greendao.dbutils.MigrationHelper.migrate(MigrationHelper.java:44) at com.example.greendao.dbutils.MySqlLiteOpenHelper.onUpgrade(MySqlLiteOpenHelper.java:23) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at org.greenrobot.greendao.database.DatabaseOpenHelper.getWritableDb(DatabaseOpenHelper.java:59) at com.example.greendao.dbutils.DaoManager.init(DaoManager.java:35) at com.example.greendao.MyApplication.onCreate(MyApplication.java:15) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5361) at android.app.ActivityThread.-wrap2(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
实测String类型不会出现该错误。而且我添加的int类型的数据也没有添加@notnull啊
这个问题困扰我一晚上了,很蛋疼,求解决
建议clone下代码运行下demo试试看,我这边刚才试了下没问题。
我只把MigrationHelper下载下来,放到里面去了。 原来是这样的, public class User{ private Long id; private int age; private String name; } 后来添加了一个int 属性 private int cdname; 在rebuild一下,然后改数据库版本号,在sync,升级就报错了 后来发现是UserDao里面创建表的时候添加了not null属性 /* Creates the underlying database table. / public static void createTable(Database db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; db.execSQL("CREATE TABLE " + constraint + "\"USER\" (" + // "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id "\"NAME\" TEXT NOT NULL UNIQUE ," + // 1: name "\"AGE\" INTEGER NOT NULL ," + // 2: age "\"CDASD\" INTEGER NOT NULL );"); // 3: cdasd } 最后在迁移数据的时候就会报错,说int类型数据不能为空,这个应该GreenDao照成的原因。
最后已经解决了这个方案~在新增的列中把int类型的数据给加上0
默认情况下并不会为NOT NULL吧,找到原因就好。
经多次测试,在实体中添加int类型新数据的时候,就会报错,无法添加数据库