android how to check Database Table Is empty or not?

3.1k views Asked by At

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

There are 2 answers

1
muhammad waqas On

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
}
3
Jigar Shekh On

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
}