Android SQL Lite help: creating databasetable

I've been trying to reference the old code to create a table. I changed the key/column names for the new table and everything was working fine. However, when I tried to change the name from "expense" (from the old code) to "transaction" (new code). I got

E/SQLiteLog: (1) near "transaction": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.cs465.groceryrun.groceryrun, PID: 7199
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cs465.groceryrun.groceryrun/com.cs465.groceryrun.groceryrun.Transactions}: android.database.sqlite.SQLiteException: near "transaction": syntax error (code 1): , while compiling: CREATE TABLE transaction (_id INTEGER PRIMARY KEY,title TEXT,person TEXT,role TEXT,date TEXT,due_date TEXT,status TEXT,rating REAL,amount REAL);

All I did was change the TABLE_NAME from:

 public static abstract class TransactionTable implements BaseColumns {

    public static final String TABLE_NAME = "expense";
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String COLUMN_NAME_PERSON = "person";
    public static final String COLUMN_NAME_ROLE = "role";
    public static final String COLUMN_NAME_DATE = "date";
    public static final String COLUMN_NAME_DUE_DATE = "due_date";
    public static final String COLUMN_NAME_STATUS = "status";
    public static final String COLUMN_NAME_RATING = "rating";
    public static final String COLUMN_NAME_AMOUNT = "amount";

    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
            _ID + " INTEGER PRIMARY KEY," +
            COLUMN_NAME_TITLE    + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_PERSON  + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_ROLE    + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_DATE    + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_DUE_DATE + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_STATUS  + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_RATING  + REAL_TYPE + COMMA_SEP +
            COLUMN_NAME_AMOUNT  + REAL_TYPE + ");";

    public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}

to:

public static abstract class TransactionTable implements BaseColumns {

    public static final String TABLE_NAME = "transaction"; //?????
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String COLUMN_NAME_PERSON = "person";
    public static final String COLUMN_NAME_ROLE = "role";
    public static final String COLUMN_NAME_DATE = "date";
    public static final String COLUMN_NAME_DUE_DATE = "due_date";
    public static final String COLUMN_NAME_STATUS = "status";
    public static final String COLUMN_NAME_RATING = "rating";
    public static final String COLUMN_NAME_AMOUNT = "amount";

    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
            _ID + " INTEGER PRIMARY KEY," +
            COLUMN_NAME_TITLE    + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_PERSON  + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_ROLE    + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_DATE    + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_DUE_DATE + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_STATUS  + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_RATING  + REAL_TYPE + COMMA_SEP +
            COLUMN_NAME_AMOUNT  + REAL_TYPE + ");";

    public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}

Does anyone know why this is happening?

Answers


transaction is a reserved keyword in SQL. Consider renaming your table to e.g. transactions.


Need Your Help

Rails: Best practice for model dependency class locations?

ruby-on-rails ruby model dependencies location

I have a rails app moving along fairly well, but the fact that I'm doing this myself means that some poor sod is eventually going to see this and say, "What the hell were you thinking? Why did you...

jQuery + Jeditable - detect when select is changed

jquery jquery-selectors jeditable

I'm using Jeditable for in-place editing. One the controls I am working with has the select type. When a user clicks on the field, the following select control is generated: