我有一个尝试创建数据库表的服务(如下所列)。我得到一个异常我不明白当执行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);
}
}
答案 0 :(得分:1)
正如guido指出的那样,你需要改变
db.execSQL(db.toString());
为:
db.execSQL(sb.toString());
它将您的db对象的字符串解释作为SQL语句发送。