0

所以,我有一个我正在制作的应用程序,可以简单地描述为一个任务列表。我正在使用 SQLite 数据库来存储所有数据。我在数据库方面遇到了各种问题,但让我们在这里坚持这个更紧迫的问题。在测试运行中,我做了一个基本的输入测试,得到了这个 logcat 条目:

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)

我看不出有什么问题。我将任务添加到数据库的方法非常简单:

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;
    }
}

谁能帮我吗?我看不到“约束失败”的地方。

4

1 回答 1

2

您正在获取 MAX id,这意味着该 id 已经存在。我认为您在返回之前忘记增加 MAX id。这是假设您的 getNewAlarmId 与 getNewTaskId 几乎完全相同

public int getNewAlarmId(SQLiteDatabase db)
{
    Cursor c = db.rawQuery("SELECT MAX(_id) FROM ALARMS", null);
    int columnID = c.getColumnIndex(ID);
    if (columnID == -1)
    {
        return 0;
    } else {
        return c.getInt(columnID) + 1;
    }
}

更新

尝试将值硬编码为 10。它是否至少运行一次,然后中断?如果是这样,我认为它可能是您的光标。你没有像 Squonk 建议的那样调用 moveToFirst

于 2012-06-13T20:52:01.580 回答