I receive a syntax error when I try to compile my tables in Sqlite

  • Replies:1
  • Answered
DRoszhart
  • Forum posts: 3

Apr 15, 2017, 5:51:07 PM via Website

A short summary of the app is that it is a quiz game. The question and answers are stored in a database. When a randomly chosen category is shown the subsequent table is chosen with the questions. I have 5 tables that I need to create but only have created three. I get an error on the other two.

Here is the error:

android.database.sqlite.SQLiteException: near "when": syntax error (code 1): , while compiling: CREATE TABLE when(_id INTEGER PRIMARY KEY ,when_question TEXT,when_multiple_choiceA TEXT,when_multiple_choiceB TEXT,when_multiple_choiceC TEXT,when_multiple_choiceD TEXT,when_answer TEXT)

Here is the construction string of the tables and the creation of then on the onCreate method:

 static final String CREATE_TABLE_WHEN = " CREATE TABLE " + TABLE_WHEN  + "("
        + Key_ID + " INTEGER PRIMARY KEY ,"
        + Key_WHEN_QUESTION + " TEXT,"
        + Key_WHEN_MULTIPLE_CHOICEA + " TEXT,"
        + Key_WHEN_MULTIPLE_CHOICEB + " TEXT,"
        + Key_WHEN_MULTIPLE_CHOICEC + " TEXT,"
        + Key_WHEN_MULTIPLE_CHOICED + " TEXT,"
        + Key_WHEN_ANSWER + " TEXT)";
private static final String CREATE_TABLEWHERE = "CREATE TABLE " + TABLE_WHERE  + " ("
        + Key_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + Key_WHERE_QUESTION + " TEXT,"
        + Key_WHERE_MULTIPLE_CHOICEA + " TEXT,"
        + Key_WHERE_MULTIPLE_CHOICEB + " TEXT,"
        + Key_WHERE_MULTIPLE_CHOICEC + " TEXT,"
        + Key_WHERE_MULTIPLE_CHOICED + " TEXT,"
        + Key_WHERE_ANSWER + " TEXT" + ")";

public void onCreate(SQLiteDatabase db) {
database = db;

    db.execSQL(CREATE_TABLEWHO);
    db.execSQL(CREATE_TABLEWHAT);
    db.execSQL(CREATE_TABLEWHY);
    db.execSQL(CREATE_TABLE_WHEN);
    db.execSQL(CREATE_TABLEWHERE);

}

Reply
James Watson
  • Forum posts: 1,584

Apr 16, 2017, 5:23:04 AM via Website

TABLE_WHEN + "(" ---> TABLE_WHEN + " (".

And your variables, TABLE_WHEN, TABLE_WHERE and Key_ID, may be invalid.
'where' is a reserved word. Field name should be 'id' other than '_id'.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

DRoszhart

Reply