mehmet mehmet - 29 days ago 21
Android Question

Sqlite Check if Table is Empty

Well, I have a databse and it has lots of table. but generally tables are empty.
I want check if a database table is empty.
IF table is empty, program will fill it.

public static long queryNumEntries (SQLiteDatabase db, String table)


I will use it but it requre API 11.

Answer

you can execute select count(*) from table and check if count> 0 then leave else populate it.

like

 SQLiteDatabase db = table.getWritableDatabase();
String count = "SELECT count(*) FROM table";
Cursor mcursor = db.rawQuery(count, null);
mcursor.moveToFirst();
int icount = mcursor.getInt(0);
if(icount>0)
//leave 
else
//populate table