public static void onCreate(SQLiteDatabase db) {
StringBuilder sb = new StringBuilder();
sb.append("CREATE TABLE " + LecturesTable.TABLE_NAME + " (");
sb.append(BaseColumns._ID + " INTEGER PRIMARY KEY, ");
sb.append(LecturesColumns.TITLE + " TEXT, ");
sb.append(LecturesColumns.SPEAKER_ID + " INTEGER, ");
sb.append(LecturesColumns.START_TIME + " DATETIME, ");
sb.append(LecturesColumns.END_TIME + " DATETIME, ");
sb.append(LecturesColumns.TYPE + " TEXT, ");
sb.append(LecturesColumns.PLACE_ID + " INTEGER, ");
sb.append(LecturesColumns.DESCRIPTION + " TEXT, ");
sb.append("FOREIGN KEY(" + LecturesColumns.SPEAKER_ID + ") "
+ "REFERENCES " + SpeakersTable.TABLE_NAME + "("
+ BaseColumns._ID + "), ");
sb.append("FOREIGN KEY(" + LecturesColumns.PLACE_ID + ") "
+ "REFERENCES " + PlacesTable.TABLE_NAME + "("
+ BaseColumns._ID + ")");
sb.append(");");
db.execSQL(sb.toString());
}
public long insert(String[] data) {
insertStatement.clearBindings();
insertStatement.bindLong(1, Long.valueOf(data[0]));
insertStatement.bindString(2, data[1]);
insertStatement.bindLong(3, Long.valueOf(data[2]));
insertStatement.bindString(4, data[3]);
insertStatement.bindString(5, data[4]);
insertStatement.bindString(6, data[5]);
insertStatement.bindLong(7, Long.valueOf(data[6]));
insertStatement.bindString(8, data[7]);
return insertStatement.executeInsert();
}
错误android.database.sqlite.SQLiteConstraintException:外键约束失败(代码19)
答案 0 :(得分:0)
您必须先启用外键支持
db.execSQL("PRAGMA foreign_keys = ON;");
您确定SpeakersTable和PlacesTable中的行具有正确的值吗?
如果失败,您可以添加约束名称以接收更准确的错误消息
"CONSTRAINT speakers FOREIGN KEY(" ...
答案 1 :(得分:-1)
错误代码19意味着您只需在插入方法中打破表格约束,例如" NOT NULL"," UNIQUE"等