Tuesday Four AM
Tuesday Four AM

Reputation: 1276

Android database creation sql code check

I'm working with my first android SQL database and I have a strong feeling that something is wrong with my database creation sql statement. What am I missing guys?

private static final String CREATE_MOVES_TABLE = 
    "CREATE TABLE " + TABLE_MOVES + "(" + 
         KEY_ID + " INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," + 
         DATE + " TEXT,"  + 
         TRANSPORT + " TEXT," + 
         MARSH + " INTEGER," + 
         TIMEHOPON + " TEXT," + 
         TIMEHOPOFF + " TEXT" + 
    ");";

I don't have a field for key_id in my constructor (I feel that it might be the weak part).

Upvotes: 0

Views: 41

Answers (1)

Canoe
Canoe

Reputation: 599

There is no need to have KEY_ID passed into your constructor, it should be a constant value.

Also, you are doing a lot of extra work to get an autoincrement on your primary key. You can remove all that generated.. stuff and replace with autoincrement

class DB {

    private static final String KEY_ID = "id";
    private static final String DATE = "date";
    private static final String TRANSPORT = "transport";
    private static final String MARSH = "marsh";
    private static final String TIMEHOPON = "timehopon";
    private static final String TIMEHOPOFF = "timehopoff"; 

    private static final String CREATE_MOVES_TABLE = 
    "CREATE TABLE " + TABLE_MOVES + "(" 
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + DATE + " TEXT,"
        + TRANSPORT + " TEXT,"
        + MARSH + " INTEGER,"
        + TIMEHOPON + " TEXT,"
        + TIMEHOPOFF + " TEXT" + ");";


    // the rest of the class...

}

Upvotes: 1

Related Questions