そのため、質問 ID 用と質問テキスト用の 2 つの列を持つテーブルに値を挿入しようとしています。挿入方法は次のとおりです。
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)