This is my query in sqlite3 by adb shell:
sqlite> SELECT round FROM prizes GROUP BY round;
100-7
However, I got a problem when translating it into Java code:
LinkedList<String> result = new LinkedList<String>();
Cursor cursor = db.rawQuery("SELECT round FROM prizes GROUP BY round", null);
if(cursor.moveToFirst()) {
do {
result.add(cursor.getString(1));
} while (cursor.moveToLast());
}
if (cursor != null && !cursor.isClosed())
cursor.close();
return (String[]) result.toArray();
When I run this code, I got an error:
ERROR/AndroidRuntime(10540): java.lang.IllegalStateException: get field slot from row 0 col 1 failed
This is because you have only 1 column returned form your query (round) and the index is zero-based
See the doc about getString
Returns the value of the requested column as a String. The result and
whether this method throws an exception when the column value is null
or the column type is not a string type is implementation-defined.
Parameters
columnIndex the zero-based index of the target column.
The below should work:
result.add(cursor.getString(0));
I believe it should be cursor.getString(0) since its a 0-based index.
Related
public Cursor selectRecords() {
Cursor mCursor = database.query("prescritions" ,new String []{"MAX(pres_no)"}, null, null, null, null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor; // iterate to get each value.
}
I created above query to read maximum value from pres_no column reside in prescription table.But i get following error
10-15 12:26:41.719: E/AndroidRuntime(629): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
getColumnIndex cannot work if you do not know the name of the column.
(And when you apply functions to a column, you result column name is not the table column name.)
In this query, the column you want is the first column, so you can drop the getColumnIndex call and simply use the column index (0) directly:
cursor.getInteger(0)
in my activity ineed to check whether the table exist or not for that im using
Cursor cursor = marksdb.rawQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='"+classt+"'", null);
im getting 1 if table exist 0 otherwise
for that i need check value in cursor for that im using
if(cursor.getCount()==1){
// get values from cursor here
callclasstb();
}
else{
tv.setVisibility(View.VISIBLE);
subjectet.setEnabled(false);
markset.setEnabled(false);
markssp.setEnabled(false);
}
but in all cases im getting value 1 because getcout() returns a value 1 r 0 and
callclasstb();
is not executing what condition i need to write in if{....} to make it execute
cursor.getCount() in your case will always return 1 (the number of "rows" in your resultset). you need to figure out what was returned via the cursor.
Cursor cursor = marksdb.rawQuery(...);
cursor.moveToFirst(); // first "row"
int nTableExists = cursor.getInt(0);
cursor.close();
if (nTableExists) { // != 0
...do something...
}
I have tried to figure this out and googled through a fair few stack overflow responses but none of them seem to address this exact problem. I am trying to return the row ID so that I can update my database.
String query= "SELECT * FROM climb_tbl WHERE climb_name ='"+climbname+"'";
Cursor cur = db.rawQuery(query, null);
cur.moveToFirst();
long id = cur.getLong(cur.getColumnIndex("_id"));
System.out.println(id);
As far as I can tell this should return the id value I need to then run the update statement, ive yet to write. Instead I get the error.....
java.lang.IllegalStateException: get field slot from row 0 col -1 failed.
Do you have _id column in your table?
Probably the cursor is empty. The cur.moveToFirst() returns true or false, so you can use it in the following way:
if (cur != null && cur.moveToFirst()) {
long id = cur.getLong(cur.getColumnIndex("_id"));
System.out.println(id);
}
I am making an android database app in which when i fetch values from database using cursor
It gives me the following exception.
java.lang.IllegalStateException: get field slot from row 0 col -1 failed
I am using the following code for fetching:
public Cursor fetchChildren(String KEY_){
Cursor c = db.rawQuery("SELECT children FROM " + DATABASE_TABLE_NAVIGATION
+ " WHERE key_ = ?", new String[] {KEY_});
return c;
}
try{
for(int k=0;k<n;k++){
db.open();
System.out.println("children are");
Cursor cursor=db.fetchChildren(keys_a);
if (cursor.moveToFirst()) // data?
{
System.out.println( cursor.getString(9));
}
}
db.close();
}
catch(Exception e)
{
System.out.println(e);
}
Use System.out.println( cursor.getString(0)) instead of System.out.println( cursor.getString(9));
Or use System.out.println(cursor.getString(cursor.getColumnIndex("children"));
Problem in your logic:
You are fetching single column from your data base. So cursor having only one column (children). As you know that column index starts from 0 (0 for first column, 1 for 2nd column ...), you have to pass 0 in getString(). Alternate way is, to search column index in cursor and pass that with getString(). as getColumnIndex(java.lang.String) calculate the index of column name in cursor, and use it within getString(int).
Hope this will help.
I have an SQL query running on an 'sqlite' database that is throwing this exception: "java.lang.IllegalStateException: get field slot from row 0 col 0 failed" when I call:
db.rawQuery( "SELECT data FROM session_data WHERE session_id = ?", new String[] { String.format("%d", session_id) } );
if (!cursor.moveToFirst()) return;
bytes[] bytes = cursor.getBlob(0);
The column exists, and if I log in using adb shell sqlite3 /path/to/database.db I can successfully execute the same query.
The resulting cursor shows that there is 1 row selected with 1 column whose name is "data". The size of the blob is about 1.5 MB - could that be it? I have verified that cursor.GetColumnIndex("data") returns 0, so the column exists.
This is running on Android 2.1 SDK. Any ideas?
The problem was that the blob was too big. It appears that cursor.getBlob(0) fails if the blob size is over a megabyte. Reading the blob in segments solved my issue:
// get length of blob data
Cursor cursor = db.rawQuery( "SELECT LENGTH(data) AS len FROM table WHERE id = ?", etc);
if (cursor == null || !cursor.moveToFirst()) throw new Exception("failed");
int size = cursor.getInt(cursor.getColumnIndexOrThrow("len"));
cursor.close();
// read a segment of the blob data
cursor = db.rawQuery( "SELECT SUBSTR(data,0,500000) AS partial FROM table WHERE id = ?", etc);
if (cursor == null || !cursor.moveToFirst()) throw new Exception("failed");
byte[] partial = cursor.getBlob(cursor.getColumnIndexOrThrow("partial"));
cursor.close();
// copy partial bytes, repeat until the whole length has been read...
I assume that you are holding a reference to the cursor from the rawQuery.
Before you get any value from the cursor you need to call cursor.moveToFirst() because the cursor is initially positioned at -1. The correct way would be:
Cursor cursor = db.rawQuery( "SELECT data FROM session_data WHERE session_id = ?", new String[] { String.format("%d", session_id) } );
if(cursor!= null && cursor.moveToFirst()){ // checking if the cursor has any rows
bytes[] bytes = cursor.getBlob(0);
}
and also make sure that your column in the database is blob type