CursorIndexOutOfBoundException: Index -1 requested, with a size of 1 - android

How do i do a select statement which retrieves 2 String and 1 integer value. I am receiving this error. My app keep crashing at cursor.getString(0); In my log, i do get this message Log.d("TAG","Row found");
Login.java
private void getQRCodeInformation(){
//database helper object
DatabaseHelper db;
//initializing views and objects
db = new DatabaseHelper(this);
Cursor cursor = db.getQRCodeInformation();
if(cursor.getCount() == 0) {
Log.d("TAG","Nothing found");
}
else{
Log.d("TAG","Row found");
if(cursor != null && cursor.moveToFirst()){
//cursor.getString(0);
//cursor.getString(1);
//cursor.getInt(1);
Log.d("TAG","Data found");
}
}
}
DatabaseHelper.java
public Cursor getQRCodeInformation(){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT "+COLUMN_0_UserDetail+" , "+COLUMN_4_UserDetail+" , "+COLUMN_5_UserDetail+ " FROM "+ TABLE_NAME_UserDetail;
Cursor c = db.rawQuery(sql, null);
return c;
}

you just need to get index of column
int index = cursor.getColumnIndex(COLUMN_NAME);
then
String columnValue = cursor.getString(index);
Good luck :)

Related

Sqlite Local database is not accurate

I'm developing android application and I'm storing some data in the local database using sqlite. When I save the database from the device file explorer and browse it using DB browser for sqlite it shows many records there which are correct. BUT i have implement a function that count number of records for specific table and it returns 0 which is wrong value.
I'm lost now cause I think the function is correct
public int numOfRecords(String tableName) {
int numOfRecords = 0;
try {
String query = "SELECT COUNT(*) FROM " + tableName ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
numOfRecords = cursor.getInt(0);
}
db.close();
}
catch (Exception ex ) {
Log.d("test" , "In exception");
}
return numOfRecords;
}
Try this code
public long getProfilesCount() {
SQLiteDatabase db = this.getReadableDatabase();
long count = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
db.close();
return count;
}
Or
public int getProfilesCount() {
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}

How to retrieve data from SQLite?

I am working on an sqlite database in which I am sure that I have inserted in sqlite correctly.
Now i am fetching data from sqlite using cursor but when I set the data to Texview, my application crashes. Here is my table:
here is my db code:
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + PRODUCT_TABLE + " WHERE ID = 23";
Cursor cursor = db.rawQuery(query,null);
return cursor;
Here is my cursor code:
Cursor cursor = dbHelper.getCheckOutProduct(Id);
if (cursor != null && cursor.getCount() > 0) {
do {
int proId = cursor.getInt(cursor.getColumnIndex("ID"));
int addressId = cursor.getInt(cursor.getColumnIndex("Price"));
int paymentTypeID = cursor.getInt(cursor.getColumnIndex("Quantity"));
int customerID = cursor.getInt(cursor.getColumnIndex("ProductID"));
Log.e("ProductId",String.valueOf(proId));
}while (cursor.moveToNext());
}
Where am I making a mistake?
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
int proId = cursor.getInt(cursor.getColumnIndex("ID"));
int addressId = cursor.getInt(cursor.getColumnIndex("Price"));
int paymentTypeID = cursor.getInt(cursor.getColumnIndex("Quantity"));
int customerID = cursor.getInt(cursor.getColumnIndex("ProductID"));
Log.e("ProductId",String.valueOf(proId));
}while (cursor.moveToNext());
}
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + PRODUCT_TABLE + " where id = 23";
Cursor cursor = db.rawQuery(query,null);
return cursor;
this is right query i guess
Where are you storing the values retrieved form cursor? For easiness, you can create a model with required attributes and set it one by one, and return a list like this:
Cursor cursor = dbHelper.getCheckOutProduct(Id);
List<Model> values = new ArrayList<>();
if (cursor != null && cursor.getCount() > 0) {
do {
int proId = cursor.getInt(cursor.getColumnIndex("ID"));
int addressId = cursor.getInt(cursor.getColumnIndex("Price"));
int paymentTypeID = cursor.getInt(cursor.getColumnIndex("Quantity"));
int customerID = cursor.getInt(cursor.getColumnIndex("ProductID"));
Model model = new Model();
model.setProdId(prodId);
//just like above line set other desired values.
values.add(model);
Log.e("ProductId",String.valueOf(proId));
}while (cursor.moveToNext());
}
return values;
Hope this helps to debug the problem.

android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0

I'm trying to use a cursor to return the most recently added entry into a database. I get the error in the title when I send the following code:
SQLiteDatabase db =getReadableDatabase();
String sql = "SELECT * FROM "+SPORTS_TABLE_NAME+" ORDER BY Sport_id DESC LIMIT 1;";
Cursor cursor = db.rawQuery(sql, null);
db.close();
return cursor.getInt(0);
Any ideas?
Your code have to be like the following:
int id = 0;
SQLiteDatabase db =getReadableDatabase();
String sql = "SELECT * FROM "+SPORTS_TABLE_NAME+" ORDER BY Sport_id DESC LIMIT 1";
Cursor cursor = db.rawQuery(sql, null);
if(cursor != null && cursor.moveToFirst()) {
id = cursor.getInt(0);
}
if(cursor != null)
db.close();
return id;

Cursor Index Out Of Bounds Exception - Android SQLite

I'm attempting to query my SQLite database but I'm getting the error "Cursor Index Out Of Bounds Exception: Index 0 requested, with a size of 0". When I run the same query in SQLite Man, I get the result I'm looking for.
public int getHighScore () {
String query = "SELECT score FROM " + SCORE_TABLE + ";";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
int score = cursor.getInt(0);
cursor.close();
return score;
}
Please check if cursor is not null then you want to fetch record from cursor.Use this line to fetch record:
cursor.getInt(cursor.getColumnIndex("score")))
use this code :
public int getHighScore () {
int score=0;
String query = "SELECT score FROM " + SCORE_TABLE + ";";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
score = cursor.getInt(cursor.getColumnIndex("score")));
}
cursor.close();
return score;
}

I can't get data from Cursor Object

I've following table structure in my SQLite Database.
In my SqliteOpenHelper class, I wrote following method.
public Form getAllForms(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+FORM_TABLE, null);
int count = cursor.getCount();
String formID = cursor.getString(0);
Form form = new Form();
form.setId(formID);
cursor.close();
db.close();
return form;
}
I'm sure there's some data in it because I already watched count in debugging mode and I saw the quantity of row that actually existing in database. But CursorIndexOutOfBoundException shows up at cursor.getString(0). plus cursor.getInt(0) and cursor.getString(1) didn't work also. What's the problem might be?
You need to move the cursor to a valid row.
Valid rows are indexed from 0 to count-1. At first the cursor will point to row index -1 i.e. the one just before the first row.
The canonical way of looping through all rows is
if (cursor.moveToFirst()) {
do {
// now access cursor columns
} while (cursor.moveToNext());
}
Try this
try {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
Form form = new Form();
form.setId(formID);
cursor.moveToNext();
}
} finally {
// database.close();
cursor.close();
dbHelper.close();
}
You need to call moveToFirst() to get to the first row before asking for values:
if ((cursor!= null) && (cursor.getCount() > 0)) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
}
So Simply use your code as
public Form getAllForms(){
Form form = new Form();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+FORM_TABLE, null);
if ((cursor != null) && (cursor.getCount() > 0)) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String formID = cursor.getString(0);
form.setId(formID);
cursor.moveToNext();
}
cursor.close();
}
db.close();
return form;
}

Categories

Resources