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);
}
Related
This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 5 years ago.
My dbhelper.java have method
public Cursor report(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM Employees", null);
return c;
}
Mainactivity.java
private List<Front1> viewReport(){
List<Front1> employeeList1 = new ArrayList<>();
Cursor c = db.report();
if (c != null && c.getCount() > 0) { // Add the addition condition
if (c.moveToFirst()) {
do{
String ids=c.getString(c.getColumnIndex("e_ids"));
String name=c.getString(c.getColumnIndex("e_name"));
Front1 front1=new Front1(ids,name1);
employeeList1.add(front1);
}while (c.moveToNext());}
}else{
Front1 front1=new Front1("101","suran");
employeeList1.add(front1);
}
I have employee table.first time application install my table has empty so application crash.if i add dumny data stop application crash but user delete dummy data again application crash.how to solve null object reference initial stage .it mean select statement used if resultset empty .i want my application run not crash.any guidence will helpfull to me.
As Cursor doesn't return null if the table is empty, you should use getCount() function to solve your problem. Your code should look like below:
if (c != null && c.getCount() > 0) { // Add the addition condition
if (c.moveToFirst()) {
do{
String ids=c.getString(c.getColumnIndex("e_ids"));
String name=c.getString(c.getColumnIndex("e_name"));
Front1 front1=new Front1(ids,name1);
employeeList1.add(front1);
}while (c.moveToNext());}
}
Your issue is that a null Cursor will not be returned from a query. Rather in the situation of no data being extracted the Cursor will be empty.
This could be checked using the Cursor getCount() method. However, it is just as easily be checked by checking the return value (true or false) of a move???? method such as moveToNext. MoveToNext can also be used to traverse numerous rows in a while loop. As such, perhaps the simplest solution is to use :-
if (c.moveToNext()) {
String ids=c.getString(c.getColumnIndex("e_ids"));
String name=c.getString(c.getColumnIndex("e_name"));
Front1 front1=new Front1(ids,name1);
employeeList1.add(front1);
}
Obviously you may need to check the size of the returned List, as it's size could be 0.
I'm trying to get records in my table. I'm using this code
if(c!=null){
if(c.moveToFirst()) {
String tipId = c.getString(c.getColumnIndex(DAO.TIP_ID)).toString().trim();
System.out.println("tipId: "+ tipId);
}else{
// Handle no rows returned
System.out.println("Handle no rows returned");
}
}else{
System.out.println("debug Cursor , cannot be created");
}
But it gives me the following exception
Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
and it refers to that line:
String tipId = c.getString(c.getColumnIndex(DAO.TIP_ID)).toString().trim();
hope anyone helps me to fix this.
Thanks in advance.
In your code
c.getColumnIndex(DAO.TIP_ID)
is returning -1. This means value of DAO.TIP_ID doesnot match with any column name of table .Check if this value is correct and matches with a column name of the table you are accessing
add following line before the line which causes problem and post the Logcat output here
Log.v("out-data",DAO.TIP_ID+" "+c.getColumnIndex(DAO.TIP_ID)+" "+c.getColumnName(0));
(add import statement import android.util.Log; in beginning of your
code)
The query selection does not contain the column DAO.TIP_ID.
You should fix this to solve this error message
"Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it."
it works in my code.
Try to position cursor by moveToFirst before reading data from it.
check for null. e,g; if (c != null && c.moveToFirst()) {}
check for count. e,g; (c != null && c.getCount() >0 && c.moveToFirst()){}
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...
}
Can anybody tell me what I am doing wrong? The table that I am querying is empty, so I think that the cursor should be empty. But instead, it returns a cursor with 1 row and 1 column. Even so, I don't see why this results in an error. All I am trying to do is return the single value (if it exists) from my query.
Can somebody help me understand this? Thank you!
Cursor cursor = db.rawQuery("SELECT max(GAME_COLUMN) FROM GAMES_TABLE", null);
if (cursor.moveToNext()) {
System.err.println("why am I here?");
nextGame = cursor.getInt(cursor.getColumnIndex("GAME_COLUMN"));
}
Bad request for field slot 0,-1. numRows = 1, numColumns = 1
This is because there is no column named GAME_COLUMN in the result. Make your query
SELECT max(GAME_COLUMN) AS GC FROM GAMES_TABLE
and then query for GC :-
Cursor cursor = db.rawQuery("SELECT max(GAME_COLUMN) AS GC FROM GAMES_TABLE", null);
if (cursor.moveToNext()) {
System.err.println("why am I here?");
nextGame = cursor.getInt(cursor.getColumnIndex("GC"));
}
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.