Android SQL Lite帮助:创建数据库表

时间:2015-11-30 18:49:51

标签: android database sqlite contract

我一直在尝试引用旧代码来创建表格。我更改了新表的键/列名称,一切正常。但是,当我试图从"费用"更改名称时(从旧代码)到"交易" (新代码)。我得到了

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);

我所做的就是改变TABLE_NAME:

 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;
}

为:

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;
}

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

transaction是SQL中的reserved keyword。考虑将您的表重命名为例如transactions