2016-12-05 88 views
1

我在我的應用程序內創建了sqlite數據庫,並試圖保存突出顯示的文本,保存是所有工作正常,甚至retreving所有高亮工作正常,但是當我查詢基於書名的高光我是收到錯誤基於字符串名稱的Sqlite數據庫查詢

這裏是我試過至今

這是我的SQLite數據庫的OnCreate中

@Override 
public void onCreate(SQLiteDatabase db) { 


    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_HIGHLIGHTS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," +KEY_BOOKNAME +" TEXT," + KEY_HIGHLIGHT+" TEXT," + KEY_TEXTSIZE + " INTEGER," 
      + KEY_PAGENO + " INTEGER" + ")"; 
    db.execSQL(CREATE_CONTACTS_TABLE); 


} 

這是工作的罰款

public List<HighlightSave> getAllHighlights(){ 

    List<HighlightSave> saveAllHighlights=new ArrayList<HighlightSave>(); 

    String selectQuery="SELECT * FROM "+TABLE_HIGHLIGHTS; 

    SQLiteDatabase db=this.getWritableDatabase(); 

    Cursor cursor=db.rawQuery(selectQuery,null); 

    if(cursor.moveToFirst()){ 

     do{ 
      HighlightSave hs=new HighlightSave(); 
      hs.setBookName(cursor.getString(1)); 
      hs.setHighlightedText(cursor.getString(2)); 

      hs.setTextSize(Integer.parseInt(cursor.getString(3))); 
      hs.setPageno(Integer.parseInt(cursor.getString(4))); 

      saveAllHighlights.add(hs); 


     }while(cursor.moveToNext()); 

    } 


    cursor.close(); 
    db.close(); 
    return saveAllHighlights; 

} 

問題是當我嘗試使用BOOKNAME像下面

public List<HighlightSave> getBookHighlights(String bookname){ 

    SQLiteDatabase db= this.getReadableDatabase(); 

    List<HighlightSave> AllHighlightsOfBook= new ArrayList<HighlightSave>(); 

     Cursor cursor = db.query(TABLE_HIGHLIGHTS, new String[] {KEY_BOOKNAME,KEY_HIGHLIGHT,KEY_TEXTSIZE,KEY_PAGENO}, KEY_BOOKNAME + "=?", new String[] { bookname }, null, null, null, null); 

    if(cursor.moveToFirst()){ 

     do{ 
      HighlightSave hs=new HighlightSave(); 
      hs.setBookName(cursor.getString(1)); 
      hs.setHighlightedText(cursor.getString(2)); 

      hs.setTextSize(Integer.parseInt(cursor.getString(3))); 
      hs.setPageno(Integer.parseInt(cursor.getString(4))); 

      AllHighlightsOfBook.add(hs); 


     }while(cursor.moveToNext()); 

    } 

    cursor.close(); 
    db.close(); 


    return AllHighlightsOfBook; 
} 

我收到此錯誤

12-05 11:03:48.025: E/AndroidRuntime(8811): FATAL EXCEPTION: main 
12-05 11:03:48.025: E/AndroidRuntime(8811): Process: com.wowio.ebookreader, PID: 8811 
12-05 11:03:48.025: E/AndroidRuntime(8811): java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.database.CursorWindow.nativeGetString(Native Method) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.database.CursorWindow.getString(CursorWindow.java:438) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at com.eminosoft.epubreader.HighlightDatabase.getBookHighlights(HighlightDatabase.java:160) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at com.eminosoft.epubreader.HighlightFragment.onActivityCreated(HighlightFragment.java:66) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.os.Handler.handleCallback(Handler.java:739) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.os.Handler.dispatchMessage(Handler.java:95) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.os.Looper.loop(Looper.java:148) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at android.app.ActivityThread.main(ActivityThread.java:5422) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at java.lang.reflect.Method.invoke(Native Method) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
12-05 11:03:48.025: E/AndroidRuntime(8811):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

查詢,所以我想有些問題,光標,初始化它裏面getBookHighlights這樣( )

String query = "Select * from "+TABLE_HIGHLIGHTS+ " Where " +KEY_BOOKNAME + " = " + bookname ; 
    Cursor cursor= db.rawQuery(query,null); 

然後我得到這個錯誤

12-05 11:20:06.514: E/AndroidRuntime(12305): FATAL EXCEPTION: main 
12-05 11:20:06.514: E/AndroidRuntime(12305): Process: com.wowio.ebookreader, PID: 12305 
12-05 11:20:06.514: E/AndroidRuntime(12305): android.database.sqlite.SQLiteException: near "Raven": syntax error (code 1): , while compiling: Select * from highlights Where book_Name = The Raven 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at com.eminosoft.epubreader.HighlightDatabase.getBookHighlights(HighlightDatabase.java:146) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at com.eminosoft.epubreader.HighlightFragment.onActivityCreated(HighlightFragment.java:66) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.os.Handler.handleCallback(Handler.java:739) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.os.Handler.dispatchMessage(Handler.java:95) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.os.Looper.loop(Looper.java:148) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at android.app.ActivityThread.main(ActivityThread.java:5422) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at java.lang.reflect.Method.invoke(Native Method) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
12-05 11:20:06.514: E/AndroidRuntime(12305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我做錯了什麼? (我卸載應用程序每次運行之前,所以不會有以前的數據庫的任何問題)

+0

你在模擬器上測試嗎? –

+0

沒有虛幻設備nexus 6p – Redman

+0

你可以在仿真器上測試一次嗎?運行你的應用程序,並打開來自adm的sqlite數據庫,並檢查表是否有數據 –

回答

2

在第一

String query = "SELECT * FROM " + TABLE_HIGHLIGHTS+ " WHERE " + KEY_BOOKNAME + " = '" + bookname + "'"; 

然後卸載舊的應用程序整頓您選擇查詢並再次運行

+1

完全工作,感謝兄弟 – Redman

1
String query="select * from "+TABLE_HIGHLIGHTS+" where "+KEY_BOOKNAME+"='"+bookname+"'"; 
+1

謝謝它的工作原理,但intellij回答它,所以接受他的答案,任何方式+1謝謝你的幫助:) – Redman

+0

亞沒有問題。謝謝+1 –

2

有關取出列值的錯誤。

列索引從Sqlite遊標中的零開始。

java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow.

所以上面的線的含義是4列不在光標對象可

你試圖用0到3,我認爲它應該得到解決。

+0

沒有嘗試這個答案,因爲上面的答案工作正常,所以我堅持任何方式感謝您的幫助+1 :) – Redman