在Android中插入数据库失败

时间:2012-05-07 18:10:42

标签: java android database sqlite

所以我试图在一个包含2列的表中插入值,一列用于问题ID,另一列用于问题文本。这是我插入它的方式:

ContentValues values = new ContentValues();
values.put(HealthyDroidQuizHelper.COLUMN_ID, 1);
values.put(HealthyDroidQuizHelper.COLUMN_QUESTION, "How are you feeling?");
database.insert(HealthyDroidQuizHelper.TABLE_QUESTION, null, values);

该方法返回-1,因此失败。我也尝试通过将insert语句硬编码到字符串中来使用database.execSQL()方法但是失败并最终导致应用程序崩溃。任何帮助将不胜感激!

编辑:这是我创建表格的方式

private static String CREATE_TABLE_QUESTION = "create table "
        + TABLE_QUESTION + "( " + COLUMN_ID 
        + " integer primary key, " + COLUMN_QUESTION + " text not null);";

    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE_QUESTION);
        //db.execSQL(CREATE_TABLE_ANSWER);
        //db.execSQL(CREATE_TABLE_ASSOCIATION);
    }

这是更新的Logcat输出

05-07 18:53:37.762: W/KeyCharacterMap(213): No keyboard for id 0
05-07 18:53:37.772: W/KeyCharacterMap(213): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-07 18:53:38.902: E/Database(213): Error inserting question=What is your name? _id=2
05-07 18:53:38.902: E/Database(213): android.database.sqlite.SQLiteException: table question has no column named question: , while compiling: INSERT INTO question(question, _id) VALUES(?, ?);
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1027)
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1413)
05-07 18:53:38.902: E/Database(213):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1286)
05-07 18:53:38.902: E/Database(213):    at ca.bcit.comp4900.QuestionDataSource.storeQuestion(QuestionDataSource.java:56)
05-07 18:53:38.902: E/Database(213):    at ca.bcit.comp4900.TestDatabaseActivity.changeText(TestDatabaseActivity.java:38)
05-07 18:53:38.902: E/Database(213):    at ca.bcit.comp4900.TestDatabaseActivity.onClick(TestDatabaseActivity.java:60)
05-07 18:53:38.902: E/Database(213):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 18:53:38.902: E/Database(213):    at java.lang.reflect.Method.invoke(Method.java:521)
05-07 18:53:38.902: E/Database(213):    at android.view.View$1.onClick(View.java:2026)
05-07 18:53:38.902: E/Database(213):    at android.view.View.performClick(View.java:2364)
05-07 18:53:38.902: E/Database(213):    at android.view.View.onTouchEvent(View.java:4179)
05-07 18:53:38.902: E/Database(213):    at android.widget.TextView.onTouchEvent(TextView.java:6541)
05-07 18:53:38.902: E/Database(213):    at android.view.View.dispatchTouchEvent(View.java:3709)
05-07 18:53:38.902: E/Database(213):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-07 18:53:38.902: E/Database(213):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-07 18:53:38.902: E/Database(213):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-07 18:53:38.902: E/Database(213):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-07 18:53:38.902: E/Database(213):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-07 18:53:38.902: E/Database(213):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-07 18:53:38.902: E/Database(213):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-07 18:53:38.902: E/Database(213):    at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-07 18:53:38.902: E/Database(213):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-07 18:53:38.902: E/Database(213):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-07 18:53:38.902: E/Database(213):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 18:53:38.902: E/Database(213):    at android.os.Looper.loop(Looper.java:123)
05-07 18:53:38.902: E/Database(213):    at android.app.ActivityThread.main(ActivityThread.java:4363)
05-07 18:53:38.902: E/Database(213):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 18:53:38.902: E/Database(213):    at java.lang.reflect.Method.invoke(Method.java:521)
05-07 18:53:38.902: E/Database(213):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-07 18:53:38.902: E/Database(213):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-07 18:53:38.902: E/Database(213):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

column_id是自动增量字段吗?如果是这样,请将其值传递为null。然后sqlite将为您分配下一个号码。如果它是自动增量并且您传递了一个值,则可能会出现重复键失败。

答案 1 :(得分:0)

您的数据库方案是什么样的?您是否已将ID列指定为“自动生成”?

答案 2 :(得分:0)

你的表创建语句正在使用文字&#34; COLUMN_QUESTION&#34;作为列名,而不是指定的值,这可能只是&#34;问题&#34;。从字符串文字中删除COLUMN_QUESTION以确保使用其值而不是文字字符串。