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;
}
Related
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;
}
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.
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 :)
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;
I wrote this function which takes in a post id and finds the last name for that post id.When I run this I get error :
database.cursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
What is wrong?
public Person_Obj GetPerson_Obj(int post_id)
{
Person_Obj obj = new Person_Obj();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_Persons + " WHERE "
+ Post_Id + " = " + post_id;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
obj.set_last_name(cursor.getString(cursor.getColumnIndex("LastName")));
return obj;
}
Try this,
SQLiteDatabase db = database.getReadableDatabase();
Cursor cursor = db.rawQuery("query", null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do
{
// write your code
} while (cursor.moveToNext());
}
database.close();