Cannot take maximum value from sql table column - android

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)

Related

sqlite cursorindexoutofbound exception

in my android application i used the following function to retrieve the column from the table..the table contains value but it has an exception.
public String[] getactivelist(){
Log.v("ppp","getactivelist");
String[] actname=new String[50];
SQLiteDatabase db = this.getWritableDatabase();
Log.v("ppp","dbcrtd");
Cursor cursor = db.rawQuery("SELECT name FROM activelist ORDER BY time ASC", null);
Log.v("ppp","aftrcurser");
int i=0;
Log.v("ppp crsr",cursor.getString(0));
if (cursor.moveToFirst()) {
do {
actname[i]=cursor.getString(0);
Log.v("ppp crsr",cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return actname;
}
the log cat shows the following error
04-04 01:19:41.170 2581-2601/com.example.pranavtv.loudspeaker V/pppīš• tryandroid.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
In your line
Log.v("ppp crsr", cursor.getString(0));
You try to get a string from the cursor. If there aren't any lines, it should throw the error observed.
You are calling the following line...
Log.v("ppp crsr",cursor.getString(0));
...before you have moved the position of your Cursor using...
if (cursor.moveToFirst()) {
By default, the position of a Cursor is initially set to be -1 which is before the first valid position as the first position which contains data is position 0.
Simply remove that line (the one before the if(...)) and you should be good to go.
On another note, in your do...while loop you are using...
actname[i]=cursor.getString(0);
...but you never increment i. Consequentially you will only ever modify actname[0] regardless of how many results are returned to the Cursor.

android & sqlite - Query a single item

I'm calling a method to see if there is a value inside the database.
String[] columns ={table.NAME};
String[] selectionsArgs = {name};
Cursor cursor = db.query(table.TABLE_NAME, columns, table.NAME+"= ?", selectionsArgs,null, null, null, null);
int index = cursor.getColumnIndex(table.ID);
Irrespective of whether the name exists or not the index is always -1.
Why ?
Irrespective of whether the name exists or not the index is always -1. Why ?
Because you didn't include table.ID in columns.
getColumnIndex() can only find columns that are there in the cursor, irrespective of whether there are any rows.
Even without the table at hand, seems a lot like you are creating the query wrong.
Strange things I see:
Using table.NAME for the columns name.
Using table.NAME also for the WHERE clause condition
Having an extra space in that "= ?"
Try with:
Cursor cursor = db.query(table.TABLE_NAME ,null ,null ,null ,null, null, null, null);
Also, check this answer out for reference: SQLiteDatabase.query method

getting table info(exist/not) from cursor

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...
}

finding _id in Android illegal state exception row 0 col -1

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);
}

java.lang.IllegalStateException error in Android Sqlite3 "group by" clause

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.

Categories

Resources