android SQLiteConstraintException:错误代码19:约束失败
我收到 android SQLiteConstraintException: 错误代码 19: 约束失败异常。我看到这个主题中有很多问题,我读了很多,但仍然无法弄清楚我做错了什么。只是我的表架构。
private static final String DATABASE_CREATE = "CREATE TABLE calls (call_ID
INTEGER AUTO INCREMENT PRIMARY KEY NOT NULL, caller_id TEXT NOT NULL, "+
caller_name TEXT NOT NULL, called_id TEXT NOT NULL, called_name TEXT NOT NULL," +
"start_time TEXT NOT NULL, end_time TEXT NOT NULL);";
我添加一行,它抛出异常。
public long addCall(String caller_id, String caller_name, String called_id,
String called_name, String start, String end) {
ContentValues initialValues = createContentValues(caller_id, caller_name,called_id,called_name, start,end);
return database.insert(DATABASE_TABLE, null, initialValues);
}
我重写了 SQLHelpers onOpen 方法,在每次打开时清除表,但这没有帮助。
public void onOpen (SQLiteDatabase database){
database.execSQL("DROP TABLE IF EXISTS calls");
onCreate(database);
Log.w(DBHelper.class.getName(),"DB onOpen");
}
更新:问题不在于,我尝试传递 null,因为我只添加这些字符串,然后收到错误。
dbAdapter.addCall("test", "test","test", "test", "test", "test");
更新 2:这是我的 createContentvalue 方法:
public static final String CALLER_ID = "caller_id";
public static final String CALLER_NAME = "caller_name";
public static final String CALLED_ID = "called_id";
public static final String CALLED_NAME = "called_name";
public static final String START = "start_time";
public static final String END = "end_time";
public static final String DATABASE_TABLE = "calls";
private ContentValues createContentValues(String caller_id, String caller_name, String called_id, String called_name, String start, String end) {
ContentValues values = new ContentValues();
values.put(CALLER_ID, caller_id);
values.put(CALLER_NAME, caller_name);
values.put(CALLED_ID, called_id);
values.put(CALLED_NAME, called_name);
values.put(START, start);
values.put(END, end);
return values;
}
I got android SQLiteConstraintException: error code 19: constraint failed exception. I saw there are tons of question in this topic, I've read a bunch of them, but still cant figure out what I did wrong. Mere is my table schema.
private static final String DATABASE_CREATE = "CREATE TABLE calls (call_ID
INTEGER AUTO INCREMENT PRIMARY KEY NOT NULL, caller_id TEXT NOT NULL, "+
caller_name TEXT NOT NULL, called_id TEXT NOT NULL, called_name TEXT NOT NULL," +
"start_time TEXT NOT NULL, end_time TEXT NOT NULL);";
I add one single row, and it throws the exception.
public long addCall(String caller_id, String caller_name, String called_id,
String called_name, String start, String end) {
ContentValues initialValues = createContentValues(caller_id, caller_name,called_id,called_name, start,end);
return database.insert(DATABASE_TABLE, null, initialValues);
}
I overrided the SQLHelpers onOpen method, to clear the table on every open, but it didnt help.
public void onOpen (SQLiteDatabase database){
database.execSQL("DROP TABLE IF EXISTS calls");
onCreate(database);
Log.w(DBHelper.class.getName(),"DB onOpen");
}
UPDATE: The problem is not that, I try to pass null, cause I only add these strings, and I got the error.
dbAdapter.addCall("test", "test","test", "test", "test", "test");
UPDATE 2: This is my createContentvalue method:
public static final String CALLER_ID = "caller_id";
public static final String CALLER_NAME = "caller_name";
public static final String CALLED_ID = "called_id";
public static final String CALLED_NAME = "called_name";
public static final String START = "start_time";
public static final String END = "end_time";
public static final String DATABASE_TABLE = "calls";
private ContentValues createContentValues(String caller_id, String caller_name, String called_id, String called_name, String start, String end) {
ContentValues values = new ContentValues();
values.put(CALLER_ID, caller_id);
values.put(CALLER_NAME, caller_name);
values.put(CALLED_ID, called_id);
values.put(CALLED_NAME, called_name);
values.put(START, start);
values.put(END, end);
return values;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查方法参数中是否有
null
值? ;-)根据更新进行编辑
哈,我知道:一个
null
值。 ;-)您使用
CALLER_NAME
两次...(顺便说一句,CALLER_ID
也是如此)根据评论进行编辑
除了
null
值之外,他试图设置主键(不小心)。这可能是主要原因(除了实际的null
值)。Checked for
null
values in your method arguments? ;-)Edit based on update
Haa, I knew it: A
null
value. ;-)You use
CALLER_NAME
twice... (btwCALLER_ID
too)Edit based on comments
Besides the
null
value, he was trying to set the primary key (accidentally). That could have been the main reason (besides the actualnull
value).