2012-05-06 28 views
0

我必須在這裏做一些愚蠢的事情,因爲我有一些非常相似的代碼可以正常工作。當我編譯並運行下面的代碼時,我得到一個android.database.sqlite.SQLiteException:Android的InsertHelper getColumnIndex()產生:android.database.sqlite.SQLiteException

05-05 20:00:29.524: E/AndroidRuntime(12785): Caused by: android.database.sqlite.SQLiteException: near "VALUES": syntax error: , while compiling: INSERT INTO bookmarks_6616 (VALUES (

這就是整個錯誤。就好像一個不完整的插入正在發送。最奇怪的是,這個異常是在第一次調用InsertHelper.getColumnIndex()時產生的,它不應該編譯插入。

代碼如下。我已經檢查過了,表格確實存在(它是空的,並在之前創建了幾行)。這些鍵也具有正確的值。我可能會切換回使用insert()而不使用insertHelper,但我希望能夠找出這個錯誤!任何幫助表示讚賞。

InsertHelper ihelper = new InsertHelper(dbHelper.mDb, tableName); 

    //Populate the DB table 
    final int idColumn = ihelper.getColumnIndex(BooksDbAdapter.KEY_CHID); 
    final int titleColumn = ihelper.getColumnIndex(BooksDbAdapter.KEY_CHAPTERTITLE); 
    final int urlColumn = ihelper.getColumnIndex(BooksDbAdapter.KEY_CHAPTERURL); 
    final int durationColumn = ihelper.getColumnIndex(BooksDbAdapter.KEY_DURATION); 

    for (RSSMessage msg : messages){ 
     if (msg.imageMessage()) { 
      dbHelper.updateImage(lvid, msg.getImageURL()); 
     } else { 
      ihelper.prepareForInsert(); 
      ihelper.bind(idColumn, chid); 
      ihelper.bind(titleColumn, msg.getChapterTitle()); 
      ihelper.bind(urlColumn, msg.getChapterURL()); 
      ihelper.bind(durationColumn, msg.getDuration()); 
      ihelper.execute(); 

      chid++; 
     } 
    } 
    ihelper.close(); 
+0

我可以看到在logcat中的錯誤,但不是爲什麼它在那裏。 'tableName'的值是什麼,以及SQL命令的其餘部分是什麼:INSERT INTO bookmarks_6616(VALUES(...'? – Sam

+0

tableName各不相同,例如contents_6348。我相信我發現了這個問題,儘管 - 在某些函數之外(我不知道是否有任何文檔)InsertHelper必須使用SQLiteDatabase.beginTransaction()包裝數據庫事務;我沒有足夠的要點來回答我自己的問題8小時,但我會在8小時後發佈修改過的代碼 – Tad

+0

修訂版:我一直是個白癡,當我看到你的評論時,我意識到我已經插入了錯誤的表:bookmarks_XXXX而不是contents_xxxx。當我添加交易代碼時意外糾正了它,只是一個愚蠢的編碼錯誤。 – Tad

回答

0

事實證明,問題只是一個不好的表名。顯然,錯誤顯示爲在getColumnIndex()上,因爲找不到匹配的列。我仍不確定爲什麼錯誤顯示不完整的INSERT語句,但編譯器可能會延遲調用getColumnIndex直到需要結果。

相關問題