Reputation: 103
I have a problem with my database in Android app. Here's some code:
private static final String QUESTIONS_TABLE = "questions";
/* questions keys */
public static final String KEY_ROWID = "_id";
public static final String KEY_TYPE = "type";
public static final String KEY_CONTENT = "content";
public static final String KEY_A = "answerA";
public static final String KEY_B = "answerB";
public static final String KEY_C = "answerC";
public static final String KEY_D = "answerD";
private static final String QUESTIONS_TABLE_CREATE =
"create table "+ QUESTIONS_TABLE +" (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_TYPE + "integer not null,"
+ KEY_CONTENT + " text not null, "
+ KEY_A + " text, "
+ KEY_B + "text, "
+ KEY_C + "text, "
+ KEY_D + "text); ";
//--- QUESTIONS SECTION ---
//--- inserts a close question ---
public long insertCloseQuestion(int type, String content, String[] answers) {
ContentValues initValues = new ContentValues();
initValues.put(KEY_TYPE, type);
initValues.put(KEY_CONTENT, content);
if(answers != null && answers.length > 0) {
initValues.put(KEY_A, answers[0]);
initValues.put(KEY_B, answers[1]);
initValues.put(KEY_C, answers[2]);
initValues.put(KEY_D, answers[3]);
}
Log.d("VM", initValues.toString());
return db.insertOrThrow(QUESTIONS_TABLE, null, initValues);
}
When I call insertCloseQuestion method, Android returns an Exception:
android.database.sqlite.SQLiteException: table questions has no column named answerD: , while compiling: INSERT INTO questions(answerD, content, answerB, answerC, type, answerA) VALUES(?, ?, ?, ?, ?, ?);
Do you have any ideas how to fix it? I know there are is a couple of topics like this, but none of the solutions help me.
Upvotes: 1
Views: 4386
Reputation: 5868
One mistake in your code is to be found in the String QUESTIONS_TABLE_CREATE Your code uses + KEY_B + "text, " which will result in the final string in answerBtext. Add a space at the beginning of text. Below code should fix it.
If there is no other mistake in your code (the parts you show here seem ok for me). This should solve your problem:
private static final String QUESTIONS_TABLE_CREATE =
"create table "+ QUESTIONS_TABLE +" (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_TYPE + " integer not null,"
+ KEY_CONTENT + " text not null, "
+ KEY_A + " text, "
+ KEY_B + " text, "
+ KEY_C + " text, "
+ KEY_D + " text); ";
Upvotes: 3
Reputation: 20936
private static final String QUESTIONS_TABLE_CREATE =
"create table "+ QUESTIONS_TABLE +" (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_TYPE + "integer not null,"
+ KEY_CONTENT + " text not null, "
+ KEY_A + " text, "
+ KEY_B + "text, "
+ KEY_C + "text, "
+ KEY_D + "text); ";
I guess there are no spaces between column name and it's type. KEY_B + "text, "
put a space before text (here and with other columns).
And do not forget to delete database after that (uninstall application from the phone)!
Upvotes: 5