-2

i want code for following concept If Data_Base_Table is empty insert the data to Data_Base_Table.Its only one time .If already data in Data_Base_Table means do nothing.I Wrote like this Here am checking DataBase Table data is empty or not is it correct or not public boolean checkDataBaseTable() { SQLiteDatabase dataBase = this.getReadableDatabase();

    String s = "SELECT * FROM"+Data_Base_Table;
    Cursor cursor = dataBase.rawQuery(s, null);
    if (cursor.moveToFirst()) {
        do {
            //int i=cursor.getColumnCount();
        }while(cursor.moveToNext());

    }
    return cursor!= null ? true : false;
}
Thej
  • 35
  • 2
  • 9

2 Answers2

4

Try with this code.

Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null,null);
if(cursor.getCount()>0)
{
     // database not empty
} else {
    // database empty
}
Jigar Shekh
  • 2,800
  • 6
  • 29
  • 54
1

I use something like this:

Cursor cursor = db.query(your-table-name, columns, null, null, null);
if(cursor.getCount() > 0) {
    //do some stuff when there is data in the table
} else {
   //do some stuff when there is no data
}
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
muhammad waqas
  • 742
  • 1
  • 5
  • 20
  • if(cursor!=null && cursor.getCount() > 0) { //just in case your cursor is null.check if it is not equal to null }else{ } – khubaib Jun 08 '15 at 06:30