所以我试图在一个有 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,因此失败。我还尝试使用 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)