下面是我在 android 中使用 ContentValues 插入的值。在插入值时我得到约束失败
ContentValues values = new ContentValues();
String time = hour_ + ":" + minute_;
String date = day_ + "/" + month_ + "/" + year_;
EditText task = (EditText) findViewById(R.id.taskDetail);
values.put(ToDoDatabase.TASK, task.getText().toString());
values.put(ToDoDatabase.TASK_TIME, time);
values.put(ToDoDatabase.TASK_DATE, date);
values.put(ToDoDatabase.TASK_NAME_FORMAT_DATE,
timeFormat.format(dateFormat.parse(date)));
toDoDatabase.insert(ToDoDatabase.TABLE_NAME, null, values);
下面给出的是来自我的 SqliteOpenHelper 子类的代码。
static final String DATABASE_NAME = "ToDo";
static final int DATABSE_VERSION = 1;
static final String ROW_ID = "_id";
static final String TASK = "task";
static final String TASK_TIME = "task_time";
static final String TASK_DATE = "task_date";
static final String TASK_STATUS = "task_status";
static final String TASK_NAME_FORMAT_DATE = "task_name_format_date";
static final String TABLE_NAME = "ToDoTable";
static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( "
+ ROW_ID + " INTEGER AUTOTINCREMENT PRIMARY KEY NOT NULL , " + TASK
+ " VARCHAR(500) NOT NULL , " + TASK_TIME
+ " VARCHAR(20) NOT NULL , " + TASK_DATE + " DATE NOT NULL , "
+ TASK_STATUS + " BOOLEAN DEFAULT 'FALSE' , "
+ TASK_NAME_FORMAT_DATE + " VARCHAR(100) NOT NULL );";
public ToDoDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABSE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_TABLE);
Log.d("Result", "Success");
} catch (Exception ex) {
Log.d("Result", "Fail");
}
}
问题是我无法弄清楚约束失败的原因。logCat 输出是
10-13 11:21:34.475: E/SQLiteDatabase(2380): Error inserting task_date=0/0/0 task_name_format_date=30,Sunday, November , '02 task=ask task_time=0:0
10-13 11:21:34.475: E/SQLiteDatabase(2380): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:117)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1724)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1597)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.softmya.softlist.TaskAdding.saveData(TaskAdding.java:129)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.softmya.softlist.TaskAdding.onOptionsItemSelected(TaskAdding.java:108)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.app.Activity.onMenuItemSelected(Activity.java:2502)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:366)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:232)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.support.v7.app.ActionBarActivityDelegateICS.onMenuItemSelected(ActionBarActivityDelegateICS.java:159)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onMenuItemSelected(ActionBarActivityDelegateICS.java:295)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:955)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
10-13 1121:34.475: E/SQLiteDatabase(2380): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:490)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:108)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.view.View.performClick(View.java:3574)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.view.View$PerformClick.run(View.java:14293)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.os.Handler.handleCallback(Handler.java:605)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.os.Looper.loop(Looper.java:137)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at java.lang.reflect.Method.invoke(Method.java:511)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-13 11:21:34.475: E/SQLiteDatabase(2380): at dalvik.system.NativeStart.main(Native Method)