AsyncQueryHandler在插入时抛出SQLException

时间:2012-08-17 00:37:37

标签: android android-sqlite

我正在尝试使用AsyncQueryHandler在一个单独的线程中插入我的数据库中的项目,但当我去尝试插入一些东西时,我得到一个SQLException

例外

08-16 18:57:13.662: E/SQLiteLog(17319): (1) table Part1s has no column named name
08-16 18:57:13.672: E/SQLiteDatabase(17319): Error inserting frame_number=2 name=Jeff game_id=1
08-16 18:57:13.672: E/SQLiteDatabase(17319): android.database.sqlite.SQLiteException: table Part1s has no column named name (code 1): , while compiling: INSERT INTO Part1s(frame_number,name,game_id) VALUES (?,?,?)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at com.tyczj.bowling.providers.Games.insert(Games.java:382)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.content.ContentProvider$Transport.insert(ContentProvider.java:201)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.content.ContentResolver.insert(ContentResolver.java:864)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.content.AsyncQueryHandler$WorkerHandler.handleMessage(AsyncQueryHandler.java:96)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.os.Looper.loop(Looper.java:137)
08-16 18:57:13.672: E/SQLiteDatabase(17319):    at android.os.HandlerThread.run(HandlerThread.java:60)
08-16 18:57:13.672: E/AndroidRuntime(17319): FATAL EXCEPTION: AsyncQueryWorker
08-16 18:57:13.672: E/AndroidRuntime(17319): android.database.SQLException: Failed to insert row into content://com.tyczj.bowling.providers.Games/Part1s
08-16 18:57:13.672: E/AndroidRuntime(17319):    at com.tyczj.bowling.providers.Games.insert(Games.java:387)
08-16 18:57:13.672: E/AndroidRuntime(17319):    at android.content.ContentProvider$Transport.insert(ContentProvider.java:201)
08-16 18:57:13.672: E/AndroidRuntime(17319):    at android.content.ContentResolver.insert(ContentResolver.java:864)
08-16 18:57:13.672: E/AndroidRuntime(17319):    at android.content.AsyncQueryHandler$WorkerHandler.handleMessage(AsyncQueryHandler.java:96)
08-16 18:57:13.672: E/AndroidRuntime(17319):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 18:57:13.672: E/AndroidRuntime(17319):    at android.os.Looper.loop(Looper.java:137)
08-16 18:57:13.672: E/AndroidRuntime(17319):    at android.os.HandlerThread.run(HandlerThread.java:60)

这是我插入的地方

        for(int i = 0;i<10;i++){
        int num = i+1;
        values.put(Games.NAMES_FRAME_NUM,num);
        values.put(Games.NAMES_GAME_ID,gameid);
        values.put(Games.NAMES_NAME,name);
        qHandler.startInsert(0, null, Games.NAMES_URI,values);
//          getContentResolver().insert(Games.NAMES_URI, values);
        names(i,name);
        values.clear();
        values.put(Games.PART1_FRAME_NUM,num);
        values.put(Games.PART1_NUM,"0");
        values.put(Games.PART1_GAME_ID,gameid);
        qHandler.startInsert(0, null, Games.PART1_URI,values);
//          getContentResolver().insert(Games.PART1_URI, values);
        values.clear();
        values.put(Games.PART2_FRAME_NUM,num);
        values.put(Games.PART2_NUM,"0");
        values.put(Games.PART2_GAME_ID,gameid);
        qHandler.startInsert(0, null, Games.PART2_URI,values);
//          getContentResolver().insert(Games.PART2_URI, values);
        values.clear();
        values.put(Games.TOTALS_FRAME_NUM,num);
        values.put(Games.TOTALS_FRAME_TOTAL,"0");
        values.put(Games.TOTALS_GAME_ID,gameid);
        qHandler.startInsert(0, null, Games.TOTALS_URI,values);
//          getContentResolver().insert(Games.TOTALS_URI, values);
        values.clear();
//          ContentValues values = new ContentValues();
        values.put(Games.POCKETS_BOWLER_ID,bowlerClickedID);
        values.put(Games.POCKETS_FRAME_NUM,i);
        values.put(Games.POCKETS_GAME_ID,gameID);
        values.put(Games.POCKETS_TEAM_ID, teamSelectedID);
        values.put(Games.POCKETS_TOURNAMENT_ID, tournamentID);
        values.put(Games.POCKETS_NUM, 0);
        values.put(Games.POCKETS_SEASON, pref.getLong(Preferences.SELECTED_SEASON, 1));
        qHandler.startInsert(0, null, Games.POCKETS_URI,values);
//          getContentResolver().insert(Games.POCKETS_URI, values);
        values.clear();
    } 

现在我知道错误显示no such column,但正如你在我的for循环中看到的那样,我清除每次插入后的值,如果我只使用正常的getContentResolver().insert()我没有问题,从来没有得到这个错误所以似乎价值观以某种方式混淆了?

并且在有人说只是使用AsyncTask之前我已经这样做了,当执行for循环时,插入仍然会阻止UI。请参阅this question了解我遇到的情况

那么无论如何会导致这种情况?

2 个答案:

答案 0 :(得分:1)

我认为在进行异步更新时,您可能正在同一个ContentValues对象上进行并发访问。

您必须等到收到onInsertComplete回调,或者更简单的方法是每次只获取ContentValues的新对象

答案 1 :(得分:0)

  

android.database.sqlite.SQLiteException:表Part1s没有列   命名名称(代码1):,编译时:INSERT INTO   Part1s(frame_number,name,game_id)VALUES(?,?,?)

仅仅因为您认为数据库中有列,并不意味着它实际存在。检查您的数据库的create语句以及表的内容和 CONFIRM 这个。只有这样才能找出你的问题。