Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

android - Trying in insert values into a database, but the insert() is failing

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are getting the MAX id, which means the id already exists. I think you forgot to increment the MAX id before you return it. This is assuming your getNewAlarmId is almost exactly the same as 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;
    }
}

UPDATE

Try hardcoding the value to like 10. Does it run at least once, then break? If so I think it may be your cursor. You didn't call moveToFirst like Squonk suggested


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...