Counting no of columns in an android sqlite table - android

Hi i wants to count the total number of columns in android sqlite table, please guide that how to achieve? what i tried is below
public int numberOfColumns(String tableName) {
Cursor cursor = database.query(tableName, null, null, null, null, null, null);
if (cursor != null) {
if (cursor.getColumnCount() > 0) {
return cursor.getColumnCount();
} else {
return 0;
}
} else {
return 0;
}
}
this works fine, what i wants to know is about it is, this approach is fine or anything better i can do!!
i visited lot of stackoverflow links but still have confusion.

This works. There are some other methods such as PRAGMA table_info(tablename) but they aren't really that much better.
Checking for cursor != null is not necessary.
Also, it's a good idea to close your cursor before exiting the method.

You need to close your cursor after leaving the function, this change should work (not tested)
public int numberOfColumns(String tableName) {
int result = 0;
Cursor cursor = null;
try {
cursor = database.query(tableName, null, null, null, null, null,
null);
result = cursor.getColumnCount();
if (result < 0) {
result = 0;
}
} finally {
if (cursor != null)
cursor.close();
}
return result;
}

Related

How to display the media title in a listview?

How can I get media titles from COntentResolver. i tried but it no works, i want to display media titles in my listView .
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
String T = cursor.getString(ind);
abcl.add(T);
Log.i("SOngName", T);
} while (cursor.moveToNext());
}
but this gives me error of IndexOutOfBondsExceptions, my error logcat is
please any one guide , thanks
well first of all you should go for Cursor Documentation in android . it will tell you that after using the cursor is not null check; you have to move cursor to the start of first row on incoming result set.
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
if(cursor.MoveToFirst()){
int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
String T = cursor.getString(ind);
abcl.add(T);
Log.i("SOngName", T);
} while (cursor.moveToNext());
}
}

Unable to fetch data while Query excuting in android Sqlite

public String[] getNewsLink(String prodcuttye) {
Cursor cursor = mDb.query(SQLITE_TABLE, new String[] { "productname" }, "ProdctType='"
+ prodcuttye + "'", null, null, null, null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("productname"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
mDb.close();
}
return result;
}
This function for fetch data in String array i am trying to excute this Query am getting data when i am call the in Sqlite browser[ select productname from MyShopingTable where ProdctType= 'Basmati Rice'] But when i try to run this Query in function i am getting cursor count 0 so i am unable to get data please tell me where am doing wrong please suggest me
Try changing your query like following :
Cursor c = mDb.query("SELECT * FROM MyShopingTable where ProdctType = ?",new String[]{prodcuttye})
See if that works in your case...

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

I get this error in my crash report but cannot figure out what it means. I think its because I'm saying to return -1 if my cursor is null. Which would cause the crash. But I'm not sure why my cursor would be null? This method in my DB class gets called in the onResume of one of my activities. Would it be better to call it in the onCreate (that way its only called once)?
public int getLastId() {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_LEVEL, KEY_MONEY, KEY_DAYSLEFT, KEY_VOTES, KEY_CORRUPTION};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns,null , null, null, null, null);
if(c != null){
c.moveToLast();
int id = c.getInt(0);
return id;
}
return -1;
}
Would this code be better?
public int getLastId() throws SQLException{
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_LEVEL, KEY_MONEY, KEY_DAYSLEFT, KEY_VOTES, KEY_CORRUPTION};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns,null , null, null, null, null);
if(c != null){
c.moveToLast();
int id = c.getInt(0);
return id;
}
return 0;
}
It seems that you have an empty cursor. Therefore also check cursor count.i.e. change
if(c != null){
c.moveToLast();
int id = c.getInt(0);
return id;
}
to
if(c != null && c.getCount() > 0){
c.moveToLast();
int id = c.getInt(0);
return id;
}
This is probably being raised by the moveToLast() call, and you are getting it because your Cursor has no rows, presumably because your table is empty.

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

CursorIndexOutOfBoundsException android

In my code, when cursor is null i encounter the exception of CursorIndexOutOfBoundsException Index 0 requested, with a size of 0 in Android
How can I fix this problem??
public Cursor getCustAccount(long custRef){
openReadable();
Cursor cursor;
cursor = database.rawQuery(" SELECT CustAccountId AS _id,AccountNo as AccountNo,BankName as BankName,BranchName as BranchName FROM tblCustAccount WHERE CustRef =" + custRef , null);
if (cursor != null)
cursor.moveToFirst();
return cursor;
}
public ArrayList<String> getCustAccountString(long custRef) {
Cursor cursor = getCustAccount(custRef);
ArrayList<String> listAccount = new ArrayList<String>();
do {
listAccount.add(String.valueOf(cursor.getInt(1)));
} while(cursor.moveToNext());
return listAccount;
}
There's no data in the cursor. You should check that moveToFirst() succeeds before any of the get...() calls:
if (cursor.moveToFirst()) {
do {
listAccount.add(String.valueOf(cursor.getInt(1)));
} while(cursor.moveToNext());
}
use:-
if(cursor != null && cursor.moveToFirst()){
return cursor;
}
return null;
public abstract boolean moveToFirst ()
Move the cursor to the first row.
This method will return false if the cursor is empty.
Returns
whether the move succeeded.
try following code:
if (cursor.getCount() > 0) {
do {
listAccount.add(String.valueOf(cursor.getInt(1)));
} while(cursor.moveToNext());
}

Categories

Resources