Android SQLiteConstraintException:错误代码19:约束失败
我已经看到了有关此异常的其他问题,但所有这些问题似乎都通过解决方案解决了,即已存在指定主键的行。对我来说似乎并非如此。我尝试用双引号替换字符串中的所有单引号,但出现了同样的问题。
我试图将一行插入到我通过执行以下操作创建的 SQLite 数据库的设置表中:
db.execSQL("DROP TABLE IF EXISTS "+Settings.SETTINGS_TABLE_NAME + ";");
db.execSQL(CREATE_MEDIA_TABLE);
db.execSQL(CREATE_SETTINGS_TABLE);
Cursor c = getAllSettings();
//If there isn't already a settings row, create a row full of defaults
if(c.getCount()==0){
ContentValues cv = new ContentValues();
cv.put(Settings.SETTING_UNIQUE_ID, "'"+Settings.uniqueID+"'");
cv.put(Settings.SETTING_DEVICE_ID, Settings.SETTING_DEVICE_ID_DEFAULT);
cv.put(Settings.SETTING_CONNECTION_PREFERENCE, Settings.SETTING_CONNECTION_PREFERENCE_DEFAULT);
cv.put(Settings.SETTING_AD_HOC_ENABLED, Settings.SETTING_AD_HOC_ENABLED_DEFAULT);
cv.put(Settings.SETTING_SERVER_ADDRESS, Settings.SETTING_SERVER_ADDRESS_DEFAULT);
cv.put(Settings.SETTING_RECORDING_MODE, Settings.SETTING_RECORDING_MODE_DEFAULT);
cv.put(Settings.SETTING_PREVIEW_ENABLED, Settings.SETTING_PREVIEW_ENABLED_DEFAULT);
cv.put(Settings.SETTING_PICTURE_RESOLUTION_X, Settings.SETTING_PICTURE_RESOLUTION_X_DEFAULT);
cv.put(Settings.SETTING_PICTURE_RESOLUTION_Y, Settings.SETTING_PICTURE_RESOLUTION_Y_DEFAULT);
cv.put(Settings.SETTING_VIDEO_RESOLUTION_X, Settings.SETTING_VIDEO_RESOLUTION_X_DEFAULT);
cv.put(Settings.SETTING_VIDEO_RESOLUTION_Y, Settings.SETTING_VIDEO_RESOLUTION_Y_DEFAULT);
cv.put(Settings.SETTING_VIDEO_FPS, Settings.SETTING_VIDEO_FPS_DEFAULT);
cv.put(Settings.SETTING_AUDIO_BITRATE_KBPS, Settings.SETTING_AUDIO_BITRATE_KBPS_DEFAULT);
cv.put(Settings.SETTING_STORE_TO_SD, Settings.SETTING_STORE_TO_SD_DEFAULT);
cv.put(Settings.SETTING_STORAGE_LIMIT_MB, Settings.SETTING_STORAGE_LIMIT_MB_DEFAULT);
this.db.insert(Settings.SETTINGS_TABLE_NAME, null, cv);
}
CREATE_SETTINGS_TABLE 字符串定义如下:
private static String CREATE_SETTINGS_TABLE = "CREATE TABLE IF NOT EXISTS " + Settings.SETTINGS_TABLE_NAME + "("
+ Settings.SETTING_UNIQUE_ID + " TEXT NOT NULL PRIMARY KEY, "
+ Settings.SETTING_DEVICE_ID + " TEXT NOT NULL , "
+ Settings.SETTING_CONNECTION_PREFERENCE + " TEXT NOT NULL CHECK("+Settings.SETTING_CONNECTION_PREFERENCE+" IN("+Settings.SETTING_CONNECTION_PREFERENCE_ALLOWED+")), "
+ Settings.SETTING_AD_HOC_ENABLED + " TEXT NOT NULL CHECK("+Settings.SETTING_AD_HOC_ENABLED+" IN("+Settings.SETTING_AD_HOC_ENABLED_ALLOWED+")), "
+ Settings.SETTING_SERVER_ADDRESS + " TEXT NOT NULL, "
+ Settings.SETTING_RECORDING_MODE + " TEXT NOT NULL CHECK("+Settings.SETTING_RECORDING_MODE+" IN("+Settings.SETTING_RECORDING_MODE_ALLOWED+")), "
+ Settings.SETTING_PREVIEW_ENABLED + " TEXT NOT NULL CHECK("+Settings.SETTING_PREVIEW_ENABLED+" IN("+Settings.SETTING_PREVIEW_ENABLED_ALLOWED+")), "
+ Settings.SETTING_PICTURE_RESOLUTION_X + " TEXT NOT NULL, "
+ Settings.SETTING_PICTURE_RESOLUTION_Y + " TEXT NOT NULL, "
+ Settings.SETTING_VIDEO_RESOLUTION_X + " TEXT NOT NULL, "
+ Settings.SETTING_VIDEO_RESOLUTION_Y + " TEXT NOT NULL, "
+ Settings.SETTING_VIDEO_FPS + " TEXT NOT NULL, "
+ Settings.SETTING_AUDIO_BITRATE_KBPS + " TEXT NOT NULL, "
+ Settings.SETTING_STORE_TO_SD + " TEXT NOT NULL CHECK("+Settings.SETTING_STORE_TO_SD+" IN("+Settings.SETTING_STORE_TO_SD_ALLOWED+")), "
+ Settings.SETTING_STORAGE_LIMIT_MB + " TEXT NOT NULL )";
但是,当我执行插入时,我总是得到:
03-19 19:37:36.974: ERROR/Database(386): Error inserting server_address='0.0.0.0' storage_limit='-1' connection='none' preview_enabled='0' sd_enabled='1' video_fps='15' audio_bitrate='96' device_id='-1' recording_mode='none' picture_resolution_x='-1' picture_resolution_y='-1' unique_id='000000000000000' adhoc_enable='0' video_resolution_x='320' video_resolution_y='240'
03-19 19:45:34.284: ERROR/Database(446): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
看起来好像我插入的所有列都不为空。该行的主键必须是唯一的,因为它是表中唯一的行。因此,我唯一能想到的是我的 CHECK 条件不成立。以下是我正在使用的预定义字符串:
public static final String SETTING_UNIQUE_ID = "unique_id";
public static final String SETTING_DEVICE_ID = "device_id";
public static final String SETTING_DEVICE_ID_DEFAULT = "'-1'";
public static final String SETTING_CONNECTION_PREFERENCE = "connection";
public static final String SETTING_CONNECTION_PREFERENCE_3G = "'3g'";
public static final String SETTING_CONNECTION_PREFERENCE_WIFI = "'wifi'";
public static final String SETTING_CONNECTION_PREFERENCE_NONE = "'none'";
public static final String SETTING_CONNECTION_PREFERENCE_ALLOWED = SETTING_CONNECTION_PREFERENCE_3G+","+SETTING_CONNECTION_PREFERENCE_WIFI+","+SETTING_CONNECTION_PREFERENCE_NONE;
public static final String SETTING_CONNECTION_PREFERENCE_DEFAULT = SETTING_CONNECTION_PREFERENCE_NONE;
public static final String SETTING_AD_HOC_ENABLED = "adhoc_enable";
public static final String SETTING_AD_HOC_ENABLED_ALLOWED = TRUE+","+FALSE;
public static final String SETTING_AD_HOC_ENABLED_DEFAULT = FALSE;
public static final String SETTING_SERVER_ADDRESS = "server_address";
public static final String SETTING_SERVER_ADDRESS_DEFAULT = "'0.0.0.0'";
public static final String SETTING_RECORDING_MODE = "recording_mode";
public static final String SETTING_RECORDING_MODE_VIDEO = "'video'";
public static final String SETTING_RECORDING_MODE_AUDIO = "'audio'";
public static final String SETTING_RECORDING_MODE_PICTURE = "'picture'";
public static final String SETTING_RECORDING_MODE_NONE = "'none'";
public static final String SETTING_RECORDING_MODE_ALLOWED = SETTING_RECORDING_MODE_VIDEO+","+SETTING_RECORDING_MODE_AUDIO+","+SETTING_RECORDING_MODE_PICTURE+","+SETTING_RECORDING_MODE_NONE;
public static final String SETTING_RECORDING_MODE_DEFAULT = SETTING_RECORDING_MODE_NONE;
public static final String SETTING_PREVIEW_ENABLED = "preview_enabled";
public static final String SETTING_PREVIEW_ENABLED_ALLOWED = TRUE+","+FALSE;
public static final String SETTING_PREVIEW_ENABLED_DEFAULT = FALSE;
public static final String SETTING_PICTURE_RESOLUTION_X = "picture_resolution_x";
public static final String SETTING_PICTURE_RESOLUTION_X_DEFAULT = "'-1'";
public static final String SETTING_PICTURE_RESOLUTION_Y = "picture_resolution_y";
public static final String SETTING_PICTURE_RESOLUTION_Y_DEFAULT = "'-1'";
public static final String SETTING_VIDEO_RESOLUTION_X = "video_resolution_x";
public static final String SETTING_VIDEO_RESOLUTION_X_DEFAULT = "'320'";
public static final String SETTING_VIDEO_RESOLUTION_Y = "video_resolution_y";
public static final String SETTING_VIDEO_RESOLUTION_Y_DEFAULT = "'240'";
public static final String SETTING_VIDEO_FPS = "video_fps";
public static final String SETTING_VIDEO_FPS_DEFAULT = "'15'";
public static final String SETTING_AUDIO_BITRATE_KBPS = "audio_bitrate";
public static final String SETTING_AUDIO_BITRATE_KBPS_DEFAULT = "'96'";
public static final String SETTING_STORE_TO_SD = "sd_enabled";
public static final String SETTING_STORE_TO_SD_ALLOWED = TRUE+","+FALSE;
public static final String SETTING_STORE_TO_SD_DEFAULT = TRUE;
public static final String SETTING_STORAGE_LIMIT_MB = "storage_limit";
public static final String SETTING_STORAGE_LIMIT_MB_DEFAULT = "'-1'";
public static final String SETTING_CLIP_LENGTH_SECONDS = "clip_length";
public static final String SETTING_CLIP_LENGTH_SECONDS_DEFAULT = "'300'";
有人知道会发生什么吗?我很困惑。提前致谢。
I've seen other questions about this exception, but all of them seem to be resolved with the solution that a row with the primary key specified already exists. This doesn't seem to be the case for me. I have tried replacing all single quotes in my strings with double quotes, but the same problem occurs.
I'm trying to insert a row into the Settings table of the SQLite database I've created by doing the following:
db.execSQL("DROP TABLE IF EXISTS "+Settings.SETTINGS_TABLE_NAME + ";");
db.execSQL(CREATE_MEDIA_TABLE);
db.execSQL(CREATE_SETTINGS_TABLE);
Cursor c = getAllSettings();
//If there isn't already a settings row, create a row full of defaults
if(c.getCount()==0){
ContentValues cv = new ContentValues();
cv.put(Settings.SETTING_UNIQUE_ID, "'"+Settings.uniqueID+"'");
cv.put(Settings.SETTING_DEVICE_ID, Settings.SETTING_DEVICE_ID_DEFAULT);
cv.put(Settings.SETTING_CONNECTION_PREFERENCE, Settings.SETTING_CONNECTION_PREFERENCE_DEFAULT);
cv.put(Settings.SETTING_AD_HOC_ENABLED, Settings.SETTING_AD_HOC_ENABLED_DEFAULT);
cv.put(Settings.SETTING_SERVER_ADDRESS, Settings.SETTING_SERVER_ADDRESS_DEFAULT);
cv.put(Settings.SETTING_RECORDING_MODE, Settings.SETTING_RECORDING_MODE_DEFAULT);
cv.put(Settings.SETTING_PREVIEW_ENABLED, Settings.SETTING_PREVIEW_ENABLED_DEFAULT);
cv.put(Settings.SETTING_PICTURE_RESOLUTION_X, Settings.SETTING_PICTURE_RESOLUTION_X_DEFAULT);
cv.put(Settings.SETTING_PICTURE_RESOLUTION_Y, Settings.SETTING_PICTURE_RESOLUTION_Y_DEFAULT);
cv.put(Settings.SETTING_VIDEO_RESOLUTION_X, Settings.SETTING_VIDEO_RESOLUTION_X_DEFAULT);
cv.put(Settings.SETTING_VIDEO_RESOLUTION_Y, Settings.SETTING_VIDEO_RESOLUTION_Y_DEFAULT);
cv.put(Settings.SETTING_VIDEO_FPS, Settings.SETTING_VIDEO_FPS_DEFAULT);
cv.put(Settings.SETTING_AUDIO_BITRATE_KBPS, Settings.SETTING_AUDIO_BITRATE_KBPS_DEFAULT);
cv.put(Settings.SETTING_STORE_TO_SD, Settings.SETTING_STORE_TO_SD_DEFAULT);
cv.put(Settings.SETTING_STORAGE_LIMIT_MB, Settings.SETTING_STORAGE_LIMIT_MB_DEFAULT);
this.db.insert(Settings.SETTINGS_TABLE_NAME, null, cv);
}
The CREATE_SETTINGS_TABLE string is defined as the following:
private static String CREATE_SETTINGS_TABLE = "CREATE TABLE IF NOT EXISTS " + Settings.SETTINGS_TABLE_NAME + "("
+ Settings.SETTING_UNIQUE_ID + " TEXT NOT NULL PRIMARY KEY, "
+ Settings.SETTING_DEVICE_ID + " TEXT NOT NULL , "
+ Settings.SETTING_CONNECTION_PREFERENCE + " TEXT NOT NULL CHECK("+Settings.SETTING_CONNECTION_PREFERENCE+" IN("+Settings.SETTING_CONNECTION_PREFERENCE_ALLOWED+")), "
+ Settings.SETTING_AD_HOC_ENABLED + " TEXT NOT NULL CHECK("+Settings.SETTING_AD_HOC_ENABLED+" IN("+Settings.SETTING_AD_HOC_ENABLED_ALLOWED+")), "
+ Settings.SETTING_SERVER_ADDRESS + " TEXT NOT NULL, "
+ Settings.SETTING_RECORDING_MODE + " TEXT NOT NULL CHECK("+Settings.SETTING_RECORDING_MODE+" IN("+Settings.SETTING_RECORDING_MODE_ALLOWED+")), "
+ Settings.SETTING_PREVIEW_ENABLED + " TEXT NOT NULL CHECK("+Settings.SETTING_PREVIEW_ENABLED+" IN("+Settings.SETTING_PREVIEW_ENABLED_ALLOWED+")), "
+ Settings.SETTING_PICTURE_RESOLUTION_X + " TEXT NOT NULL, "
+ Settings.SETTING_PICTURE_RESOLUTION_Y + " TEXT NOT NULL, "
+ Settings.SETTING_VIDEO_RESOLUTION_X + " TEXT NOT NULL, "
+ Settings.SETTING_VIDEO_RESOLUTION_Y + " TEXT NOT NULL, "
+ Settings.SETTING_VIDEO_FPS + " TEXT NOT NULL, "
+ Settings.SETTING_AUDIO_BITRATE_KBPS + " TEXT NOT NULL, "
+ Settings.SETTING_STORE_TO_SD + " TEXT NOT NULL CHECK("+Settings.SETTING_STORE_TO_SD+" IN("+Settings.SETTING_STORE_TO_SD_ALLOWED+")), "
+ Settings.SETTING_STORAGE_LIMIT_MB + " TEXT NOT NULL )";
However, when I execute my insert, I always get:
03-19 19:37:36.974: ERROR/Database(386): Error inserting server_address='0.0.0.0' storage_limit='-1' connection='none' preview_enabled='0' sd_enabled='1' video_fps='15' audio_bitrate='96' device_id='-1' recording_mode='none' picture_resolution_x='-1' picture_resolution_y='-1' unique_id='000000000000000' adhoc_enable='0' video_resolution_x='320' video_resolution_y='240'
03-19 19:45:34.284: ERROR/Database(446): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
It seems as if all the columns in my insert are not null. The row's primary key HAS to be unique, because it's the only row in the table. Therefore, the only thing I can think of is my CHECK conditions aren't true. Here are the predefined strings I'm using:
public static final String SETTING_UNIQUE_ID = "unique_id";
public static final String SETTING_DEVICE_ID = "device_id";
public static final String SETTING_DEVICE_ID_DEFAULT = "'-1'";
public static final String SETTING_CONNECTION_PREFERENCE = "connection";
public static final String SETTING_CONNECTION_PREFERENCE_3G = "'3g'";
public static final String SETTING_CONNECTION_PREFERENCE_WIFI = "'wifi'";
public static final String SETTING_CONNECTION_PREFERENCE_NONE = "'none'";
public static final String SETTING_CONNECTION_PREFERENCE_ALLOWED = SETTING_CONNECTION_PREFERENCE_3G+","+SETTING_CONNECTION_PREFERENCE_WIFI+","+SETTING_CONNECTION_PREFERENCE_NONE;
public static final String SETTING_CONNECTION_PREFERENCE_DEFAULT = SETTING_CONNECTION_PREFERENCE_NONE;
public static final String SETTING_AD_HOC_ENABLED = "adhoc_enable";
public static final String SETTING_AD_HOC_ENABLED_ALLOWED = TRUE+","+FALSE;
public static final String SETTING_AD_HOC_ENABLED_DEFAULT = FALSE;
public static final String SETTING_SERVER_ADDRESS = "server_address";
public static final String SETTING_SERVER_ADDRESS_DEFAULT = "'0.0.0.0'";
public static final String SETTING_RECORDING_MODE = "recording_mode";
public static final String SETTING_RECORDING_MODE_VIDEO = "'video'";
public static final String SETTING_RECORDING_MODE_AUDIO = "'audio'";
public static final String SETTING_RECORDING_MODE_PICTURE = "'picture'";
public static final String SETTING_RECORDING_MODE_NONE = "'none'";
public static final String SETTING_RECORDING_MODE_ALLOWED = SETTING_RECORDING_MODE_VIDEO+","+SETTING_RECORDING_MODE_AUDIO+","+SETTING_RECORDING_MODE_PICTURE+","+SETTING_RECORDING_MODE_NONE;
public static final String SETTING_RECORDING_MODE_DEFAULT = SETTING_RECORDING_MODE_NONE;
public static final String SETTING_PREVIEW_ENABLED = "preview_enabled";
public static final String SETTING_PREVIEW_ENABLED_ALLOWED = TRUE+","+FALSE;
public static final String SETTING_PREVIEW_ENABLED_DEFAULT = FALSE;
public static final String SETTING_PICTURE_RESOLUTION_X = "picture_resolution_x";
public static final String SETTING_PICTURE_RESOLUTION_X_DEFAULT = "'-1'";
public static final String SETTING_PICTURE_RESOLUTION_Y = "picture_resolution_y";
public static final String SETTING_PICTURE_RESOLUTION_Y_DEFAULT = "'-1'";
public static final String SETTING_VIDEO_RESOLUTION_X = "video_resolution_x";
public static final String SETTING_VIDEO_RESOLUTION_X_DEFAULT = "'320'";
public static final String SETTING_VIDEO_RESOLUTION_Y = "video_resolution_y";
public static final String SETTING_VIDEO_RESOLUTION_Y_DEFAULT = "'240'";
public static final String SETTING_VIDEO_FPS = "video_fps";
public static final String SETTING_VIDEO_FPS_DEFAULT = "'15'";
public static final String SETTING_AUDIO_BITRATE_KBPS = "audio_bitrate";
public static final String SETTING_AUDIO_BITRATE_KBPS_DEFAULT = "'96'";
public static final String SETTING_STORE_TO_SD = "sd_enabled";
public static final String SETTING_STORE_TO_SD_ALLOWED = TRUE+","+FALSE;
public static final String SETTING_STORE_TO_SD_DEFAULT = TRUE;
public static final String SETTING_STORAGE_LIMIT_MB = "storage_limit";
public static final String SETTING_STORAGE_LIMIT_MB_DEFAULT = "'-1'";
public static final String SETTING_CLIP_LENGTH_SECONDS = "clip_length";
public static final String SETTING_CLIP_LENGTH_SECONDS_DEFAULT = "'300'";
Does anyone see what could be going on? I'm stumped. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来 android put(string,string) 方法必须已经为您在值字符串周围附加引号...一旦我取出我在值字符串周围输入的手动引号(但将它们保留在“允许”中) ") 字符串我传递给 CHECK,问题解决了。
It seems that the android put(string,string) method must already append quotes around your value string for you... as soon as I took the manual quotes I had been entering around my value strings out (but kept them in the "ALLOWED") strings I was passing to CHECK, the problem was solved.
您是否尝试过转义单引号?例如,
如果这不能解决问题,我会将其全部分解,直到您只有一列,然后使其正常工作。然后一次添加每一列,以便您可以找出导致问题的列。
Have you tried escaping your single quotes? e.g.
If that doesn't fix it, I would break it all down until you only have a single column, and get that to work. Then add each column one at a time so you can figure out which one(s) are causing the problem.