我在应用程序内创建了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;
}
问题是当我尝试使用如下书名进行查询时
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):致命异常:主
12-05 11:03:48.025:E / AndroidRuntime(8811):进程:com.wowio.ebookreader,PID:8811
12-05 11:03:48.025:E / AndroidRuntime(8811):java.lang.IllegalStateException:无法从CursorWindow读取行0,第4行。在访问游标之前,请确保游标已正确初始化。
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.database.CursorWindow.nativeGetString(本机方法)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.database.CursorWindow.getString(CursorWindow.java:438)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于com.eminosoft.epubreader.HighlightDatabase.getBookHighlights(HighlightDatabase.java:160)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于com.eminosoft.epubreader.HighlightFragment.onActivityCreated(HighlightFragment.java:66)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:446)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.os.Handler.handleCallback(Handler.java:739)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.os.Handler.dispatchMessage(Handler.java:95)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.os.Looper.loop(Looper.java:148)
12-05 11:03:48.025:E / AndroidRuntime(8811):位于android.app.ActivityThread.main(ActivityThread.java:5422)
12-05 11:03:48.025:E / AndroidRuntime(8811):at java.lang.reflect.Method.invoke(本机方法)
12-05 11:03:48.025:E / AndroidRuntime(8811):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
12-05 11:03:48.025:E / AndroidRuntime(8811):在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)
我究竟做错了什么? (我在每次运行前都卸载了应用程序,因此以前的数据库不会有任何问题)
最佳答案
首先纠正您的SELECT查询
String query = "SELECT * FROM " + TABLE_HIGHLIGHTS+ " WHERE " + KEY_BOOKNAME + " = '" + bookname + "'";
然后卸载旧的应用程序并再次运行。