我的android项目中有以下execSQL,它会引发语法错误并导致我的应用程序崩溃。我无法弄清楚为什么。
private static final String TABLE_QUESTIONS = "question";
private static final String KEY_ID_Q = "id";
private static final String KEY_QUESTION_Q = "question";
private static final String KEY_ANSWER1_Q = "answer1";
private static final String KEY_ANSWER2_Q = "answer2";
private static final String KEY_ANSWER3_Q = "answer3";
private static final String KEY_ANSWER4_Q = "answer4";
private static final String KEY_TYPE_Q = "type_Q";
[... ]
String CREATE_QUESTIONS_TABLE = "CREATE TABLE " + TABLE_QUESTIONS + "("
+ KEY_ID_Q + " INTEGER PRIMARY KEY,"
+ KEY_QUESTION_Q + " TEXT,"
+ KEY_ANSWER1_Q + " INTEGER,"
+ KEY_ANSWER2_Q + " INTEGER,"
+ KEY_ANSWER3_Q + " INTEGER,"
+ KEY_ANSWER4_Q + " INTEGER,"
+ KEY_TYPE_Q + "INTEGER" + ")";
db.execSQL(CREATE_QUESTIONS_TABLE);
db.execSQL("INSERT INTO " + TABLE_QUESTIONS +" (" + KEY_QUESTION_Q + ", " + KEY_ANSWER1_Q + ", " + KEY_ANSWER2_Q+ ", " + KEY_ANSWER3_Q + ", " + KEY_ANSWER4_Q + ", " + KEY_TYPE_Q + ") VALUES (" + QUESTION[a] + ", "+(a*4+1)+", "+(a*4+2)+", "+(a*4+3)+", "+(a*4+4)+", "+q_type+");");
logcat的:
09-28 21:27:26.629: E/Database(1070): Failure 1 (table question has no column named type_Q) on 0x2c3868 when preparing 'INSERT INTO question (question, answer1, answer2, answer3, answer4, type_Q) VALUES ('AnyString', 1, 2, 3, 4, 1);'.
我希望你们中的一些人能够弄明白:)
答案 0 :(得分:2)
就像你忘了一个空间一样简单;
+ KEY_TYPE_Q + "INTEGER" + ")";
应该是
+ KEY_TYPE_Q + " INTEGER" + ")";
...否则你会尝试创建一个名为type_QINTEGER
但没有类型的字段。