我遇到导致SQLiteConstraintException的NULLABLE外键问题。使用以下命令启用外键:
PRAGMA foreign_keys = ON
使用以下语句创建表:
CREATE TABLE conversation (_id INTEGER PRIMARY KEY);
CREATE TABLE message (_id INTEGER PRIMARY KEY, conversation_id INTEGER, FOREIGN KEY(conversation_id) REFERENCES conversation (_id) ON DELETE CASCADE);
这些陈述令人沮丧以改善阅读。
正如您所看到的,外键' conversation_id'被声明为NULLABLE,因此我不会被迫将每条消息链接到一个对话(例如,当创建一个没有相应对话的草稿消息时)。
当我尝试使用conversation_id插入新消息时,一切都很顺利。当我尝试没有时,我得到以下错误:
11-07 16:44:55.190 20582-20582/com.miawe.miabe E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.miawe.miabe, PID: 20582
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.miawe.miabe/com.miawe.miabe.gui.MainActivity}: android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
at com.miawe.miabe.database.DatabaseDataSource.insert(DatabaseDataSource.java:244)
at com.miawe.miabe.database.DatabaseDataSource.createDummyData(DatabaseDataSource.java:307)
at com.miawe.miabe.gui.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
似乎sqlite3忽略了我的NULL约束并强制每条消息都有一个外键。如何使列conversation_id接受NULL值?
答案 0 :(得分:6)
您需要明确地将NULL
传递给插入内容。