Android sqlite语法错误

时间:2014-02-20 11:31:15

标签: android sqlite

我遇到了这个问题,虽然效果很好,但是当我打电话给GameStore.commitGame(...)时会出现问题

02-20 14:53:50.457: I/SqliteDatabaseCpp(6438): sqlite returned: error code = 1, msg = near ")": syntax error, db=/data/data/com.dwaik.minesweeper/databases/minesweeper_db
02-20 14:53:50.457: D/AndroidRuntime(6438): Shutting down VM
02-20 14:53:50.467: W/dalvikvm(6438): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
02-20 14:53:50.487: E/AndroidRuntime(6438): FATAL EXCEPTION: main
02-20 14:53:50.487: E/AndroidRuntime(6438): java.lang.RuntimeException: Unable to pause activity {com.dwaik.minesweeper/com.dwaik.minesweeper.view.MainActivity}: android.database.sqlite.SQLiteException: near ")": syntax error: , while compiling: CREATE TABLE grid(id INTEGER PRIMARY KEY,x INTEGER,y INTEGER,num_bombs INTEGER,num_flags INTEGER,num_true_flags INTEGER,num_hidden_nodes INTEGER,)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2726)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2682)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2660)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread.access$800(ActivityThread.java:127)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1169)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.os.Looper.loop(Looper.java:137)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread.main(ActivityThread.java:4441)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at java.lang.reflect.Method.invoke(Method.java:511)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at dalvik.system.NativeStart.main(Native Method)
02-20 14:53:50.487: E/AndroidRuntime(6438): Caused by: android.database.sqlite.SQLiteException: near ")": syntax error: , while compiling: CREATE TABLE grid(id INTEGER PRIMARY KEY,x INTEGER,y INTEGER,num_bombs INTEGER,num_flags INTEGER,num_true_flags INTEGER,num_hidden_nodes INTEGER,)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at com.dwaik.minesweeper.db.DatabaseHelper.createTables(DatabaseHelper.java:78)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at com.dwaik.minesweeper.model.GameStore.commitGame(GameStore.java:17)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at com.dwaik.minesweeper.view.MainActivity.onPause(MainActivity.java:98)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.Activity.performPause(Activity.java:4563)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1195)
02-20 14:53:50.487: E/AndroidRuntime(6438):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2713)
02-20 14:53:50.487: E/AndroidRuntime(6438):     ... 12 more

这是我的DBHelper类

public class DatabaseHelper extends SQLiteOpenHelper
{
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "minesweeper_db";
    private static final String TABLE_GRID = "grid";
    private static final String TABLE_NODE = "node";
    private static final String KEY_X="x";
    private static final String KEY_Y="y";
    private static final String KEY_ID="id";
    private static final String KEY_GRID_NUM_BOMBS="num_bombs";
    private static final String KEY_GRID_NUM_FLAGS="num_flags";
    private static final String KEY_GRID_NUM_TRUE_FLAGS="num_true_flags";
    private static final String KEY_GRID_NUM_HIDDEN_NODES="num_hidden_nodes";
    private static final String KEY_NODE_ADJECANT="adjecant";
    private static final String KEY_NODE_RENDER_FLAG="render_flag";
    private static final String KEY_NODE_BOMB="is_bomb";
    private static final String KEY_NODE_REVEALED="is_revealed";

    // Table Create Statements
    private static final String CREATE_TABLE_GRID = "CREATE TABLE "+ TABLE_GRID + 
            "(" + KEY_ID + " INTEGER PRIMARY KEY," +
            KEY_X + " INTEGER," +
            KEY_Y + " INTEGER," + 
            KEY_GRID_NUM_BOMBS + " INTEGER," + 
            KEY_GRID_NUM_FLAGS + " INTEGER," + 
            KEY_GRID_NUM_TRUE_FLAGS + " INTEGER," + 
            KEY_GRID_NUM_HIDDEN_NODES + " INTEGER," + ")";

    private static final String CREATE_TABLE_NODE = "CREATE TABLE "+ TABLE_NODE + 
            "(" + KEY_ID + " INTEGER PRIMARY KEY," +
            KEY_X + " INTEGER," +
            KEY_Y + " INTEGER," + 
            KEY_NODE_ADJECANT + " INTEGER," + 
            KEY_NODE_RENDER_FLAG + " INTEGER," + 
            KEY_NODE_BOMB + " INTEGER," + 
            KEY_NODE_REVEALED + " INTEGER," + ")";

    private static final String DELETE_TABLE_GRID = "delete * from "+ TABLE_GRID;
    private static final String DELETE_TABLE_NODE = "delete * from "+ TABLE_NODE;


    public DatabaseHelper(Context context)
    {    super(context, DATABASE_NAME, null , DATABASE_VERSION);    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {    createTables();    }

    public void createTables()
    {
        SQLiteDatabase db = getWritableDatabase();
        Log.v("creating tables", "....");
        db.execSQL(CREATE_TABLE_GRID);
        db.execSQL(CREATE_TABLE_NODE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GRID);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NODE);
        onCreate(db);
    }

    //---------delete data------------
    public void deleteAllData()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(DELETE_TABLE_GRID);
        db.execSQL(DELETE_TABLE_NODE);
    }

    public void closeDB()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        if (db != null && db.isOpen())
            db.close();
    }

}

和我保存/恢复数据的GameStore

public class GameStore
{
    public static void commitGame( Context context , Game game )
    {
        DatabaseHelper db = new DatabaseHelper(context.getApplicationContext());
        //db.deleteAllData();
        db.createTables();
        //db.closeDB();
        ....
    }
}

我在那里错过了什么?

3 个答案:

答案 0 :(得分:2)

只需从以下查询的最后一行删除","

 private static final String CREATE_TABLE_GRID = "CREATE TABLE "+ TABLE_GRID + 
    ............................   
     KEY_GRID_NUM_HIDDEN_NODES + " INTEGER," + ")"; <------ Remove "," from last

将其更改如下:

 private static final String CREATE_TABLE_GRID = "CREATE TABLE "+ TABLE_GRID + 
              ............................   
        KEY_GRID_NUM_HIDDEN_NODES + " INTEGER " + ")"; <----- Check here

您对另一个查询也犯了同样的错误:

 private static final String CREATE_TABLE_NODE = "CREATE TABLE "+ TABLE_NODE + 
        "(" + KEY_ID + " INTEGER PRIMARY KEY," +
       .........................................
        KEY_NODE_REVEALED + " INTEGER," + ")"; <------ Remove "," from last

将其更改如下:

 private static final String CREATE_TABLE_NODE = "CREATE TABLE "+ TABLE_NODE + 
        "(" + KEY_ID + " INTEGER PRIMARY KEY," +
       .........................................
        KEY_NODE_REVEALED + " INTEGER" + ")";  <----- Check here

答案 1 :(得分:2)

您在CREATE_TABLE个查询中的结束括号之前添加了一个额外的(,) ...将其删除...或者使用您的“<\ n”重新获得以下查询。< / p>

private static final String CREATE_TABLE_GRID = "CREATE TABLE "+ TABLE_GRID + 
        "(" + KEY_ID + " INTEGER PRIMARY KEY," +
        KEY_X + " INTEGER," +
        KEY_Y + " INTEGER," + 
        KEY_GRID_NUM_BOMBS + " INTEGER," + 
        KEY_GRID_NUM_FLAGS + " INTEGER," + 
        KEY_GRID_NUM_TRUE_FLAGS + " INTEGER," + 
        KEY_GRID_NUM_HIDDEN_NODES + " INTEGER" + ")";

private static final String CREATE_TABLE_NODE = "CREATE TABLE "+ TABLE_NODE + 
        "(" + KEY_ID + " INTEGER PRIMARY KEY," +
        KEY_X + " INTEGER," +
        KEY_Y + " INTEGER," + 
        KEY_NODE_ADJECANT + " INTEGER," + 
        KEY_NODE_RENDER_FLAG + " INTEGER," + 
        KEY_NODE_BOMB + " INTEGER," + 
        KEY_NODE_REVEALED + " INTEGER" + ")";

答案 2 :(得分:2)

错误发生在

 KEY_GRID_NUM_HIDDEN_NODES + " INTEGER," + ")";

最后一列不得以逗号结尾。但在你的情况下," INTEGER,"以逗号结尾。

应该是

 KEY_GRID_NUM_HIDDEN_NODES + " INTEGER" + ")";

KEY_GRID_NUM_HIDDEN_NODES + " INTEGER )";

所以你需要将两个常量修改为

   private static final String CREATE_TABLE_GRID = "CREATE TABLE "+ TABLE_GRID + 
            "(" + KEY_ID + " INTEGER PRIMARY KEY," +
            KEY_X + " INTEGER," +
            KEY_Y + " INTEGER," + 
            KEY_GRID_NUM_BOMBS + " INTEGER," + 
            KEY_GRID_NUM_FLAGS + " INTEGER," + 
            KEY_GRID_NUM_TRUE_FLAGS + " INTEGER," + 
            KEY_GRID_NUM_HIDDEN_NODES + " INTEGER" + ")"; // OR can write + " INTEGER )";

    private static final String CREATE_TABLE_NODE = "CREATE TABLE "+ TABLE_NODE + 
            "(" + KEY_ID + " INTEGER PRIMARY KEY," +
            KEY_X + " INTEGER," +
            KEY_Y + " INTEGER," + 
            KEY_NODE_ADJECANT + " INTEGER," + 
            KEY_NODE_RENDER_FLAG + " INTEGER," + 
            KEY_NODE_BOMB + " INTEGER," + 
            KEY_NODE_REVEALED + " INTEGER" + ")"; // OR can write + " INTEGER )";