Search code examples
androidsqliteopenhelper

sqlite exception while creating db


Im trying to create table using the following sqlite query.

private static final String CREATE_EMPLOYEE_TABLE = "CREATE TABLE " + TABLE_EMPLOYEE + "("
            + KEY_DOMAIN_ID + " INTEGER PRIMARY KEY,"
            + KEY_FROM + " TEXT,"
            + KEY_TO + " TEXT" + ")";

private static final String CREATE_GROUPS_TABLE = "CREATE TABLE " + TABLE_GROUPS + "("
        + KEY_USERID + " INTEGER PRIMARY KEY,"
        + KEY_NAME + " TEXT,"
        + KEY_SESSION_TOKEN + " TEXT,"
        + KEY_GROUP_ID + " TEXT,"
        + KEY_GROUP_NAME + " TEXT" + ")";`

Im using the following code to add elements to db,

db.addEmployees(new Employee(id,from,to));
db.addGroup(new Group(Integer.valueOf(userId),name,session,groupId,groupName));

Both of those queries throws following error message

android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: CREATE TABLE group(userId INTEGER PRIMARY KEY,name TEXT,sessionToken TEXT,groupId TEXT,groupName TEXT)


Solution

  • Rename variable

    String TABLE_GROUPS = "group"

    to something like

    String TABLE_GROUPS = "groups"

    group is reserved keyword in sql.

    Check here the list of reserved keywords in SQLite