Sqlite database query based on a string name
<p>I created sqlite database inside my app and trying to save highlighted text , Saving is all working fine , even retrieving all highlights is working fine , but when I query for highlights based on bookname I am getting an error.</p>

<p>Here is what I tried so far - this is my <code>oncreate</code> of sqlite database:</p>

<pre><code>@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);  
}
</code></pre>

<p>This is working fine</p>

<pre><code>  public List&lt;HighlightSave&gt; getAllHighlights() {

    List&lt;HighlightSave&gt; saveAllHighlights=new ArrayList&lt;HighlightSave&gt;();

    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;
}
</code></pre>

<p>Problem is when I try to query using a bookname like below</p>

<pre><code> public List&lt;HighlightSave&gt; getBookHighlights(String bookname) {

    SQLiteDatabase db= this.getReadableDatabase();

    List&lt;HighlightSave&gt; AllHighlightsOfBook= new ArrayList&lt;HighlightSave&gt;();

       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; 
}
</code></pre>

<p>I get this error</p>

<blockquote>
  <p>12-05 11:03:48.025: E/AndroidRuntime(8811): FATAL EXCEPTION: main<br>
  12-05 11:03:48.025: E/AndroidRuntime(8811): Process: com.wowio.ebookreader, PID: 8811<br>
  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.</p>
  
  <p>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)</p>
</blockquote>

<p>so i thought some problem with cursor so initialised it this way inside getBookHighlights()</p>

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

<p>then i am getting this error</p>

<pre><code>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.&lt;init&gt;(SQLiteProgram.java:58)
12-05 11:20:06.514: E/AndroidRuntime(12305):    at android.database.sqlite.SQLiteQuery.&lt;init&gt;(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)
</code></pre>

<p>What am I doing wrong?  (I uninstalled app before every run so there won't be any problem with previous database)</p>