我无法在Android Sqlite数据库中插入记录, 抛出的异常如下所示:
07-28 14:22:08.781: E/Database(403): Error inserting id=1 phone_number=999000 name=John
07-28 14:22:08.781: E/Database(403): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
07-28 14:22:08.781: E/Database(403): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
07-28 14:22:08.781: E/Database(403): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
07-28 14:22:08.781: E/Database(403): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
07-28 14:22:08.781: E/Database(403): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
07-28 14:22:08.781: E/Database(403): at com.example.dbtest.DBHelper.insertContact(DBHelper.java:47)
07-28 14:22:08.781: E/Database(403): at com.example.dbtest.MainActivity.onCreate(MainActivity.java:22)
07-28 14:22:08.781: E/Database(403): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 14:22:08.781: E/Database(403): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-28 14:22:08.781: E/Database(403): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-28 14:22:08.781: E/Database(403): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-28 14:22:08.781: E/Database(403): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-28 14:22:08.781: E/Database(403): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 14:22:08.781: E/Database(403): at android.os.Looper.loop(Looper.java:123)
07-28 14:22:08.781: E/Database(403): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-28 14:22:08.781: E/Database(403): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 14:22:08.781: E/Database(403): at java.lang.reflect.Method.invoke(Method.java:507)
07-28 14:22:08.781: E/Database(403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-28 14:22:08.781: E/Database(403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-28 14:22:08.781: E/Database(403): at dalvik.system.NativeStart.main(Native Method)
07-28 14:22:08.781: D/ContactsMaster ::(403): inserted
insertQuery函数如下所列:
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
public void insertContact(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, "1");
values.put(KEY_NAME, "Rajan");
values.put(KEY_PH_NO, "9990008973");
db.insert(TABLE_NAME, null, values);
db.close();
}
执行时的代码返回异常。数据库已成功创建,但我无法将记录插入其中。
答案 0 :(得分:3)
删除values.put(KEY_ID, "1");
,因为您的KEY_ID
是主键。和主键必须是唯一的。
当您再次插入记录时,已经找到一条记录为旧ID的记录。所以,它'给出了约束失败的错误。
答案 1 :(得分:1)
INTEGER PRIMARY KEY会生成自己,您不应该指定它。要解决您的问题,只需删除此行:
values.put(KEY_ID, "1");
如果您想要获取该ID,则在您拨打insert()这样的时候会返回该ID:
Long id = db.insert(TABLE_NAME, null, values);
有关如何在Android中使用SQLite的更多信息建议您查看this教程:
答案 2 :(得分:1)
因为如果记录ID已存在,当SQLite尝试按ID插入时会出现错误。
尝试使用一些解决方案:
添加自动增量:KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,
删除:values.put(KEY_ID, "1");
答案 3 :(得分:0)
我不确定你对KEY_ID有什么不对。主键应该能够存储任何内容,只要它对每行唯一。
一列可以是主键,而不是自动增加的整数,它应该可以正常工作
但是当你在android中工作时,如果你将名为“_id”的列定义为主键,那么它会更好。你得到这样的东西。
String KEY_ID = "_id";
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
答案 4 :(得分:0)
由于您已定义ID主键,因此无法为其分配值!只需删除该部分:
values.put(KEY_ID, "1");
强烈建议在数据库的ID字段中添加“AUTO_INCERMENT”以制作唯一ID。
答案 5 :(得分:0)
代替此
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
我写了这个
String CREATE_CONTACTS_TABLE = "CREATE TABLE TABLE_NAME (" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";