Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid CursorWindow returns only 1 row, 1 column
    text
    copied!<p>I'm attempting to create an app to pull the sqlite data from a preexisting database, one copied to a directory before the app is run. The database contains upwards of 2000 rows and around 20 columns.</p> <p>DBAdapter class.</p> <pre><code>package com.t3hh4xx0r.gmusicsniper; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBAdapter { public static final String KEY_ROWID = "Id"; public static final String KEY_TITLE = "Title"; private static final String DATABASE_NAME = Constants.gMusicSniperDir + Constants.musicDB; private static final String DATABASE_TABLE = "MUSIC"; private static final int DATABASE_VERSION = 37; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } } //---opens the database--- public DBAdapter open() throws SQLException { db = SQLiteDatabase.openDatabase(DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY); return this; } //---closes the database--- public void close() { DBHelper.close(); } //---retrieves a particular title--- public Cursor getTitle(int songFinalValue) throws SQLException { Cursor mCursor = db.rawQuery("SELECT Title FROM MUSIC WHERE Id = 5899;", null); //db.query(DATABASE_TABLE, new String [] {KEY_TITLE}, KEY_ROWID + " = \'" + songFinalValue + "\'", null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } } </code></pre> <p>Method where data is -supposed- to be retrieved. songFinalValue is in this test case 5899, though ive hardcoded the value into the query statement to be sure that the issue wasnt with any uninitialized variables.</p> <p>songFinalValue refers to the first column, Id. The value that Im trying to return is of the corresponding Title, column 13.</p> <pre><code> private void getTrackName(String songFinal) { int songFinalValue = Integer.parseInt(songFinal); DBAdapter db = new DBAdapter(this); //---get a title--- db.open(); Cursor c = db.getTitle(songFinalValue); while (c.isAfterLast() == false &amp;&amp; c != null) { c.getString(13); // will fetch you the data c.moveToNext(); //DisplayTitle(c); String cS = String.valueOf(c); Log.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", cS); } db.close(); } </code></pre> <p>When ran, it errors with </p> <pre><code>E/CursorWindow(17998): Failed to read row 0, column 13 from a CursorWindow which has 1 rows, 1 columns. E/su (18033): sudb - Database closed W/System.err(17998): java.lang.IllegalStateException: Couldn't read row 0, col 13 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. </code></pre> <p>Ive been working on this for a while, and have visited every guide, tutorial, and have poured over many SOF questions, and I cant seem to figure out the issue. </p> <p>This is my first foray into SQLite databases, and also one of my first apps. Im assuming the error is something easily solved by anyone with experience, it usually ends up being some stupid mistake on my part, though I cant for the life of me figure out what it would be.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload