本文介绍了创建SQLiteException当数据库 - 近SQLiteException" Android和QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图创建数据库表服务(如下所示)。在我得到一个例外是在执行中PlayerTable的的onCreate SQL()的时候,我不明白。所有code如下。基本上,该服务启动时,获取一个SQLiteDatabase对象,而这又是呼吁ClueBuddyOpenHelper的onCreate(),然后调用的onCreate()在PlayerTable。在SQL执行时发生异常。任何帮助将是AP preciated。

  06-03 00:47:43.043:E / AndroidRuntime(4347):致命异常:IntentService [NewGameService]
06-03 00:47:43.043:E / AndroidRuntime(4347):android.database.sqlite.SQLiteException:近机器人:语法错误
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.database.sqlite.SQLiteStatement.native_executeSql(本机方法)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:90)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
06-03 00:47:43.043:E / AndroidRuntime(4347):在ws.hamacher.cluebuddy.db.PlayerTable.onCreate(PlayerTable.java:32)
06-03 00:47:43.043:E / AndroidRuntime(4347):在ws.hamacher.cluebuddy.db.ClueBuddyOpenHelper.onCreate(ClueBuddyOpenHelper.java:30)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
06-03 00:47:43.043:E / AndroidRuntime(4347):在ws.hamacher.cluebuddy.service.NewGameService.onHandleIntent(NewGameService.java:18)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.os.Handler.dispatchMessage(Handler.java:99)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.os.Looper.loop(Looper.java:137)
06-03 00:47:43.043:E / AndroidRuntime(4347):在android.os.HandlerThread.run(HandlerThread.java:60)

code:

 公共类ClueBuddyOpenHelper扩展SQLiteOpenHelper {    私有静态ClueBuddyOpenHelper例如= NULL;    私人ClueBuddyOpenHelper(最终上下文的背景下){
        超(背景下,context.getString(R.string.db_name),空,R.string.db_version);
    }    公共静态ClueBuddyOpenHelper的getInstance(上下文的背景下){
         如果(例如== NULL){
             例如=新ClueBuddyOpenHelper(context.getApplicationContext());
         }
         返回实例;
    }    @覆盖
    公共无效的OnOpen(最终SQLiteDatabase DB){
        super.onOpen(DB);
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        PlayerTable.onCreate(DB);
        MoveTable.onCreate(DB);
        TurnTable.onCreate(DB);
        PersonTable.onCreate(DB);
        WeaponTable.onCreate(DB);
        RoomTable.onCreate(DB);    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        // TODO自动生成方法存根    }}
公共类PlayerTable {    公共静态最后弦乐TABLE_NAME =播放器;    公共静态类PlayerColumns {
        公共静态最后弦乐PLAYER_ID =player_id;
        公共静态最后的字符串标记=令牌;
        公共静态最后弦乐NUMBER_OF_CARDS =number_of_cards;
        公共静态最后弦乐USER_IS_PLAYER =user_is_player;
        公共静态最后弦乐ACTIVE =主动;
    }    公共静态无效的onCreate(SQLiteDatabase DB){
        StringBuilder的SB =新的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(文字);
        sb.append(PlayerColumns.ACTIVE);
        sb.append(文本NOT NULL););
        db.execSQL(db.toString());
    }}
公共类NewGameService扩展IntentService {    公共NewGameService(){
        超级(NewGameService);
    }    @覆盖
    保护无效onHandleIntent(意向意图){        SQLiteDatabase DB = ClueBuddyOpenHelper.getInstance(本).getWritableDatabase();
        db.execSQL(DELETE FROM运动员;);
        db.execSQL(DELETE FROM转;);
        db.execSQL(DELETE FROM举动;);
        db.execSQL(DELETE FROM人;);
        db.execSQL(DELETE FROM武器;);
        db.execSQL(DELETE FROM室;);
        意图broadcastIntent =新的Intent();
        broadcastIntent.setAction(BroadcastReceivers.NE​​W_GAME_BROADCAST_RECEIVER.getName());
        //broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        sendBroadcast(broadcastIntent);
    }}


解决方案

作为由Guido指出的那样,您需要更改

  db.execSQL(db.toString());

  db.execSQL(sb.toString());

据发送您的数据库对象的字符串间pretation作为你的SQL语句。

I have a service (listed below) which is trying to create database tables. The am getting an exception I don't understand when the sql within onCreate() in PlayerTable is executed. All the code is listed below. Basically, the service starts up, gets an SQLiteDatabase object, which in turn is calling onCreate() on ClueBuddyOpenHelper which then calls onCreate() in PlayerTable. The exception occurs when the sql executes. Any help would be appreciated.

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)

Code:

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

}
解决方案

As pointed out by guido, you need to change

db.execSQL(db.toString());

to:

db.execSQL(sb.toString());

It is sending the string interpretation of your db object as your SQL statement.

这篇关于创建SQLiteException当数据库 - 近SQLiteException" Android和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:13