我在我的應用程序內創建了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)
我做錯了什麼? (我卸載應用程序每次運行之前,所以不會有以前的數據庫的任何問題)
你在模擬器上測試嗎? –
沒有虛幻設備nexus 6p – Redman
你可以在仿真器上測試一次嗎?運行你的應用程序,並打開來自adm的sqlite數據庫,並檢查表是否有數據 –