So, I have an application I'm making that can be simply described as a task list. I'm using an SQLite database to store all the data. I'm having various problems with the database, but let's stick with this more pressing bit here. On a test run, I did a basic input test, and got this logcat entry:
06-13 01:49:33.868: E/Database(482): Error inserting ISCHECKED=0 NOTE=This is a test. _id=0 TASK=Hello World
06-13 01:49:33.868: E/Database(482): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
06-13 01:49:33.868: E/Database(482): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
06-13 01:49:33.868: E/Database(482): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
06-13 01:49:33.868: E/Database(482): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
06-13 01:49:33.868: E/Database(482): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
06-13 01:49:33.868: E/Database(482): at com.mowdownDevelopments.nagTasks.NagTasksDatabaseHelper.addTask(NagTasksDatabaseHelper.java:68)
06-13 01:49:33.868: E/Database(482): at com.mowdownDevelopments.nagTasks.NagTasksAddTaskFragment.save(NagTasksAddTaskFragment.java:65)
06-13 01:49:33.868: E/Database(482): at com.mowdownDevelopments.nagTasks.NagTasksAddTaskFragment.onOptionsItemSelected(NagTasksAddTaskFragment.java:54)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.app.SherlockFragment.onOptionsItemSelected(SherlockFragment.java:67)
06-13 01:49:33.868: E/Database(482): at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:1919)
06-13 01:49:33.868: E/Database(482): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:357)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:288)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:586)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:526)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:738)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:510)
06-13 01:49:33.868: E/Database(482): at com.actionbarsherlock.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
06-13 01:49:33.868: E/Database(482): at android.view.View.performClick(View.java:2485)
06-13 01:49:33.868: E/Database(482): at android.view.View$PerformClick.run(View.java:9080)
06-13 01:49:33.868: E/Database(482): at android.os.Handler.handleCallback(Handler.java:587)
06-13 01:49:33.868: E/Database(482): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 01:49:33.868: E/Database(482): at android.os.Looper.loop(Looper.java:123)
06-13 01:49:33.868: E/Database(482): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 01:49:33.868: E/Database(482): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 01:49:33.868: E/Database(482): at java.lang.reflect.Method.invoke(Method.java:507)
06-13 01:49:33.868: E/Database(482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 01:49:33.868: E/Database(482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 01:49:33.868: E/Database(482): at dalvik.system.NativeStart.main(Native Method)
I can't see what the problem is. My method for adding the task to the database is about as simple as it gets:
public void addTask(String title, String notes)
{
SQLiteDatabase db = getWritableDatabase();
int newestID = getNewTaskId(db);
ContentValues values = new ContentValues();
values.put("_id", newestID);
values.put("TASK", title);
values.put("NOTE", notes);
values.put("ISCHECKED", 0);
db.insert("TASKS", null, values);
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS TASKS (_id INTEGER PRIMARY KEY, TASK TEXT, NOTE TEXT, ISCHECKED INTEGER);");
}
public int getNewTaskId(SQLiteDatabase db)
{
Cursor c = db.rawQuery("SELECT MAX(_id) FROM TASKS", null);
int columnID = c.getColumnIndex(ID);
if (columnID == -1)
{
return 0;
} else {
return c.getInt(columnID) +1;
}
}
Can anyone help me out? I don't see where I'm getting a "Constraint failed".
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…