1

我有一项服务(如下所列)正在尝试创建数据库表。当执行 PlayerTable 中的 onCreate() 中的 sql 时,我遇到了一个我不明白的异常。下面列出了所有代码。基本上,服务启动,获取一个 SQLiteDatabase 对象,该对象依次调用 ClueBuddyOpenHelper 上的 onCreate(),然后调用 PlayerTable 中的 onCreate()。sql执行时发生异常。任何帮助,将不胜感激。

06-03 00:47:43.043: E/AndroidRuntime(4347): FATAL EXCEPTION: IntentService[NewGameService]
06-03 00:47:43.043: E/AndroidRuntime(4347): android.database.sqlite.SQLiteException: near "android": syntax error
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:90)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at ws.hamacher.cluebuddy.db.PlayerTable.onCreate(PlayerTable.java:32)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at ws.hamacher.cluebuddy.db.ClueBuddyOpenHelper.onCreate(ClueBuddyOpenHelper.java:30)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at ws.hamacher.cluebuddy.service.NewGameService.onHandleIntent(NewGameService.java:18)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.os.Looper.loop(Looper.java:137)
06-03 00:47:43.043: E/AndroidRuntime(4347):     at android.os.HandlerThread.run(HandlerThread.java:60)

代码:

public class ClueBuddyOpenHelper extends SQLiteOpenHelper {

    private static ClueBuddyOpenHelper instance = null;

    private ClueBuddyOpenHelper(final Context context) {
        super(context, context.getString(R.string.db_name), null, R.string.db_version);
    }

    public static ClueBuddyOpenHelper getInstance(Context context) {
         if (instance == null) {
             instance = new ClueBuddyOpenHelper(context.getApplicationContext());
         }
         return instance;
    }

    @Override
    public void onOpen(final SQLiteDatabase db) {
        super.onOpen(db);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        PlayerTable.onCreate(db);
        MoveTable.onCreate(db);
        TurnTable.onCreate(db);
        PersonTable.onCreate(db);
        WeaponTable.onCreate(db);
        RoomTable.onCreate(db);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}


public class PlayerTable {

    public static final String TABLE_NAME = "player";

    public static class PlayerColumns {
        public static final String PLAYER_ID = "player_id";
        public static final String TOKEN = "token";
        public static final String NUMBER_OF_CARDS = "number_of_cards";
        public static final String USER_IS_PLAYER = "user_is_player";
        public static final String ACTIVE = "active";
    }

    public static void onCreate(SQLiteDatabase db) {
        StringBuilder sb = new StringBuilder("");
        sb.append("CREATE TABLE ");
        sb.append(PlayerTable.TABLE_NAME);
        sb.append(" (");
        sb.append(PlayerColumns.PLAYER_ID);
        sb.append(" INTEGER PRIMARY KEY, ");
        sb.append(PlayerColumns.TOKEN);
        sb.append(" TEXT UNIQUE NOT NULL, ");
        sb.append(PlayerColumns.NUMBER_OF_CARDS);
        sb.append(" INTEGER NOT NULL, ");
        sb.append(PlayerColumns.USER_IS_PLAYER);
        sb.append(" TEXT, ");
        sb.append(PlayerColumns.ACTIVE);
        sb.append(" TEXT NOT NULL);");
        db.execSQL(db.toString());
    }

}


public class NewGameService extends IntentService {

    public NewGameService() {
        super("NewGameService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        SQLiteDatabase db = ClueBuddyOpenHelper.getInstance(this).getWritableDatabase();
        db.execSQL("DELETE FROM player;");
        db.execSQL("DELETE FROM turn;");
        db.execSQL("DELETE FROM move;");
        db.execSQL("DELETE FROM person;");
        db.execSQL("DELETE FROM weapon;");
        db.execSQL("DELETE FROM room;");


        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(BroadcastReceivers.NEW_GAME_BROADCAST_RECEIVER.getName());
        //broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        sendBroadcast(broadcastIntent);
    }

}
4

1 回答 1

1

正如guido所指出的,你需要改变

db.execSQL(db.toString());

至:

db.execSQL(sb.toString());

它将您的数据库对象的字符串解释作为您的 SQL 语句发送。

于 2013-06-16T03:18:58.633 回答