在SQLite android中创建表查询语法错误

时间:2015-11-30 07:17:32

标签: android sqlite android-sqlite

我正在编写一个使用sqlite数据库的android应用程序,现在我在执行update table的create query时遇到语法错误:

 // Logcat tag
private static final String LOG = "DatabaseHelper";

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "budget";

// Table Names
private static final String TABLE_CATEGORY = "category";
private static final String TABLE_UPDATE = "update";
private static final String TABLE_CATEGORY_UPDATE = "category_update";

// Common column names
private static final String KEY_ID = "id";

// Category Table - column nmaes
private static final String KEY_TYPE = "TYPE";


// Update Table - column names
private static final String KEY_MONEY = "money";
private static final String KEY_LOCATION = "location";
private static final String IMAGE_PATH = "image_path";
// CATEGORY_UPDATE Table - column names
private static final String KEY_CATEGORY_ID = "category_id";
private static final String KEY_UPDATE_ID = "update_id";


// Table Create Statements
// Category table create statement
private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE if not exists "
        + TABLE_CATEGORY + "(" + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_TYPE
        + " TEXT"+ ");";

// Update table create statement
private static final String CREATE_TABLE_UPDATE = "CREATE TABLE if not exists " + TABLE_UPDATE
        + "(" + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_MONEY + " INTEGER,"
        + KEY_LOCATION + " TEXT," + IMAGE_PATH +" TEXT);";

// CATEGORY_UPDATE table create statement
private static final String CREATE_TABLE_CATEGORY_UPDATE = "CREATE TABLE if not exists "
        + TABLE_CATEGORY_UPDATE + "(" + KEY_ID + " INTEGER PRIMARY KEY autoincrement,"
        + KEY_CATEGORY_ID + " INTEGER," + KEY_UPDATE_ID + " INTEGER );";

所有创建查询的排序如下:

@Override
public void onCreate(SQLiteDatabase db) {

    // creating required tables
    db.execSQL(CREATE_TABLE_CATEGORY_UPDATE);
    db.execSQL(CREATE_TABLE_CATEGORY);
    db.execSQL(CREATE_TABLE_UPDATE);

}

我在创建更新表中只收到语法错误,这里是打印错误:

11-30 01:59:20.841 24880-24880/com.example.hassanalmusajjen.tba E/SQLiteLog: (1) near "update": syntax error
11-30 01:59:20.845 24880-24880/com.example.hassanalmusajjen.tba W/System.err: android.database.sqlite.SQLiteException: near "update": syntax error (code 1): , while compiling: CREATE TABLE if not exists update(id INTEGER PRIMARY KEY,money INTEGER,location TEXT,image_path TEXT);
11-30 01:59:20.846 24880-24880/com.example.hassanalmusajjen.tba W/System.err:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-30 01:59:20.846 24880-24880/com.example.hassanalmusajjen.tba W/System.err:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-30 01:59:20.846 24880-24880/com.example.hassanalmusajjen.tba W/System.err:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-30 01:59:20.846 24880-24880/com.example.hassanalmusajjen.tba W/System.err:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588
11-30 01:59:20.846 24880-24880/com.example.hassanalmusajjen.tba W/System.err:     at com.example.hassanalmusajjen.tba.localDB.DatabaseHelper.onCreate(DatabaseHelper.java:77)

1 个答案:

答案 0 :(得分:3)

UPDATE,或者在您的情况update中,是SQLite中的undefined behavior。为您的表选择一个不同的名称。