2011-05-01 20 views
6

我的應用程序需要返回包含一堆引號的遊標,具體取決於所選作者。現在,我很難編寫應用程序,因此它只會返回一個作者的引號。Android中的光標錯誤告訴我列不存在

錯誤發生在我的遊標適配器參數中,設置「to」列,它告訴我該列「quote」不存在。

我已經嘗試將參數更改爲KEY_QUOTE,甚至嘗試更改實際數據庫列的名稱,但現在仍然喜悅。

我在做什麼錯?

以下是在此操作中使用的代碼塊。

  1. 創建數據庫表,行情
  2. 填充此表
  3. 查詢報價
  4. 打開數據庫連接,創建遊標(這裏我得到了實際的錯誤,用「從」參數)

1.

private static final String CREATE_QUOTE_TABLE = 

      "create table " + QUOTES_TABLE + 
      " (_id integer primary key autoincrement, " + 
      "auth_name text not null, " + 
      "myQuote text not null, " + 
      "category text not null);"; 

2.

public long populateQuotes(){ 

    ContentValues initialValues = new ContentValues(); 

    long[] rowIds = new long[authorName.length]; 

    // Add wilson quotes category: Anthropology 
    for(int i = 0; i < 3; i++){ 
     initialValues.put(KEY_AUTHNAME, authorName[3]); 
     initialValues.put(KEY_QUOTE, quoteRAW[i]); 
     initialValues.put(KEY_CATEGORY, category[0]); 
     rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE, null, initialValues); 
    } 
    // Add wilson quotes category: RAW 
    for(int i = 3; i < 5; i++){ 
     initialValues.put(KEY_AUTHNAME, authorName[3]); 
     initialValues.put(KEY_QUOTE, quoteRAW[i]); 
     initialValues.put(KEY_CATEGORY, category[1]); 
     rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE, null, initialValues); 

    } 
    // Add wilson quotes category: Philosophy 
    for(int i = 5; i < 11; i++){ 
     initialValues.put(KEY_AUTHNAME, authorName[3]); 
     initialValues.put(KEY_QUOTE, quoteRAW[i]); 
     initialValues.put(KEY_CATEGORY, category[2]); 
     rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE, null, initialValues); 

    } 
    // Add wilson quotes category: General Semantics 
    for(int i = 11; i < 12; i++){ 
     initialValues.put(KEY_AUTHNAME, authorName[3]); 
     initialValues.put(KEY_QUOTE, quoteRAW[i]); 
     initialValues.put(KEY_CATEGORY, category[3]); 
     rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE, null, initialValues); 

    } 
    // Add wilson quotes category: Humor 
    for(int i = 11; i < 12; i++){ 
     initialValues.put(KEY_AUTHNAME, authorName[3]); 
     initialValues.put(KEY_QUOTE, quoteRAW[i]); 
     initialValues.put(KEY_CATEGORY, category[4]); 
     rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE, null, initialValues); 

    } 

    return rowIds[0]; 
} 

3.

public Cursor getQuotes(){ 

    /* 
    return qmDB.query(QUOTES_TABLE, new String[]{ 
      KEY_QUOTE 
      }, 
      KEY_AUTHNAME + "=" + "'Robert Anton Wilson'", 
      null, 
      null, 
      null, 
      null);*/ 



    //String who = authorName[position]; 
    return qmDB.rawQuery("SELECT _id as _id from Quotes WHERE auth_name = ?", 
      new String[]{"'Robert Anton Wilson'"}); 

} 

4.

// Create DB connection and open. 
    dbm = new MyDBManager(this); 
    dbm.open(); 

    // Call for the quotes to be queried, store results in cursor. 
    myCursor = dbm.getQuotes(); 
    myCursor.moveToFirst(); 

    // Column quote does not exist? 
    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.quoterow,    myCursor, new String[]{"myQuote"}, new int[]{R.id.quote}); 
    this.setListAdapter(mAdapter); 

    myCursor.close(); 

爲了進一步澄清,我已經包含錯誤日誌:

05-01 18:38:13.876: ERROR/AndroidRuntime(1455): FATAL EXCEPTION: main 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.QuoteMachine/com.QuoteMachine.Quote}: java.lang.IllegalArgumentException: column 'myQuote' does not exist 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.ActivityThread.access$2300(ActivityThread.java:135) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.os.Looper.loop(Looper.java:144) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at dalvik.system.NativeStart.main(Native Method) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455): Caused by: java.lang.IllegalArgumentException: column 'myQuote' does not exist 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:87) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at com.QuoteMachine.Quote.onCreate(Quote.java:42) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):  ... 11 more 

回答

6

在CREATE_QUOTE_TABLE中,您使用「quote text not null」,並在logcat中顯示:Caused by: java.lang.IllegalArgumentException: column 'myQuote' does not existKEY_QUOTE的價值是多少?它應該匹配表中的列名稱。

測試此查詢:

return qmDB.rawQuery("SELECT _id, myQuote, auth_name, category FROM Quotes WHERE auth_name=?", 
    new String[]{"'Robert Anton Wilson'"}); 

我相信你的問題不在於查詢試圖檢索是不存在的列。我相信問題是你創建的遊標沒有你的適配器所需的所有信息(即遊標沒有列,而不是表)。

+0

嗨Aleadam,logcat是在我採納了Heiko的建議並更改了列名後添加的。我已經更新了我的原始帖子以匹配logcat。 – MindDrip 2011-05-01 18:22:59

+0

@MindDrip這可能太明顯了,但是你叫'qmDB.execSQL(CREATE_QUOTE_TABLE);'?如果不是'KEY_QUOTE'你檢索'KEY_CATEGORY',你會得到什麼?你有類似的錯誤嗎? – Aleadam 2011-05-01 18:44:21

+0

是我在我的oncreate()調用了CREATE_QUOTE_TABLE後,我調用execSQL()創建作者表。另外,當我嘗試檢索KEY_CEGEGORY時,它給了我同樣的錯誤,類別列不存在..這導致我認爲我的SQL查詢存在問題。正如你所看到的,我已經在上面列出了它,但我不是100%自信的,因爲我必須爲光標適配器「選擇_id as _id」,但我不明白這是如何選擇引號列的。 – MindDrip 2011-05-01 19:45:58

0

你的 '報價' 聲明可能會失敗,因爲'quote()'是函數的名稱 - 請參閱core functions

嘗試使用例如'myQuote'作爲列名。

+0

嘿Heiko,我已經嘗試過這種方法,並沒有結果,但謝謝你的建議。 – MindDrip 2011-05-01 17:32:58

+0

我認爲你還使用列名myQuote重新創建數據庫,並在查詢中使用它? – 2011-05-01 18:19:50

+0

你推定正確! – MindDrip 2011-05-01 19:36:52

2

我不是100%確定,但它可能是因爲documentation指出用於CursorAdapter的光標需要包含一個引用「_id」列。

+0

嗨dmon,你是正確的遊標適配器需要引用和_id列,並且我得到一個沒有_id列錯誤之前此問題。它通過使用「 SELECT _id as _Id ....「在我的查詢。除非這種方法是不正確的,仍然導致錯誤? – MindDrip 2011-05-01 19:38:42

+0

+1 @dmon我把注意力集中在桌子上,通過閱讀你的答案,我意識到問題出現在遊標中,而不是表格中。 – Aleadam 2011-05-01 20:50:56

+0

良好的團隊精神@Aleadam! – dmon 2011-05-01 21:52:09

相關問題