31

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.

mehmet
  • 1,558
  • 5
  • 30
  • 41
  • Note that the `queryNumEntries(db:table:)` method was added in API level 1; it's the `queryNumEntries(db:table:selection)` and `queryNumEntries(db:table:selection:selectionArgs)` methods which were added in API level 11. See the [android.database.DatabaseUtils](https://developer.android.com/reference/android/database/DatabaseUtils) Android Developers page for reference. – Adil Hussain Nov 27 '20 at 10:01

7 Answers7

48

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
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • well it is working. I have modified a little bit but I dont know It will accour problem in the future. I have deleted first line `SQLiteDatabase db = table.getWritableDatabase();` and at the beginning of class I have defined my database as **static** like this: `private static SQLiteDatabase mDb;` – mehmet Mar 25 '14 at 14:57
  • you can declare that way too but why do u define static.? – Waqar Ahmed Mar 25 '14 at 17:53
  • because of eclipse suggestion – mehmet Mar 26 '14 at 07:18
  • 1
    this is not a optimal solutions, As all knows count method take more process to execute a query, Please use select exists query instead of count() – Ashish Dwivedi Aug 28 '14 at 11:07
  • 1
    This works good. I put mine in a method that returns an int and assigned the cursor value to the returning value. Then I used this method in a if statement based on its returning value and if it equaled 0 do stuff else do nothing so to speak. I had to add some logging to the if sections just to track its steps in the DDMS and filtered the tags for clear reading tag:Database Operations. i.e. Log.v(TAG, "Some table is not empty"); thanks @Waqar Ahamed – natur3 Dec 30 '14 at 17:47
  • Why `getWritableDatabase()`? You are just reading the data. – Apurva Jul 24 '15 at 04:27
  • @Apurva..because if table will empty then we need to write data in table, so for that i use getWritableDatabase. – Waqar Ahmed Jul 24 '15 at 07:46
11

Do a SELECT COUNT:

boolean empty = true
Cursor cur = db.rawQuery("SELECT COUNT(*) FROM YOURTABLE", null);
if (cur != null && cur.moveToFirst()) {
    empty = (cur.getInt (0) == 0);
}
cur.close();

return empty;
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
10
 public boolean isEmpty(String TableName){

    SQLiteDatabase database = this.getReadableDatabase();
    long NoOfRows = DatabaseUtils.queryNumEntries(database,TableName);

    if (NoOfRows == 0){
        return true;
    } else {
        return false;
    }
}
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Mahesh Uligade
  • 597
  • 8
  • 17
  • 2
    Instead of the `if` clause you can simply do `return NoOfRows == 0;` – LoMaPh Jun 29 '18 at 21:12
  • For the curious: the `DatabaseUtils` class referenced in this answer is the [android.database.DatabaseUtils](https://developer.android.com/reference/android/database/DatabaseUtils) class. – Adil Hussain Nov 27 '20 at 10:03
6

Optimal Solutions

public boolean  isMasterEmpty() {

    boolean flag;
    String quString = "select exists(select 1 from " + TABLE_MASTERS  + ");";

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(quString, null);
    cursor.moveToFirst();
    int count= cursor.getInt(0);
    if (count ==1) {
        flag =  false;
    } else {
        flag = true;
    } 
    cursor.close();
    db.close();

    return flag;
    }
Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78
3

Here is a better option:

public boolean validateIfTableHasData(SQLiteDatabase myDatabase,String tableName){
    Cursor c = myDatabase.rawQuery("SELECT * FROM " + tableName,null);
    return c.moveToFirst();
}
1

This is how you can do it -

if(checkTable("TABLE"))
{
  //table exists fill data.
}

Method to check table -

public static boolean checkTable(String table) {
Cursor cur2 = dbAdapter.rawQuery("select name from sqlite_master where name='"
        + table + "'", null);

if (cur2.getCount() != 0) {
    if (!cur2.isClosed())
        cur2.close();
    return true;
} else {
    if (!cur2.isClosed())
        cur2.close();
    return false;
}
}
sjain
  • 23,126
  • 28
  • 107
  • 185
0

I think, this solution is better:

    boolean flag;

    DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext(), DatabaseHelper.DATABASE_NAME, null, DatabaseHelper.DATABASE_VERSION);
    try {
        sqLiteDatabase = databaseHelper.getWritableDatabase();
    } catch (SQLException ex) {
        sqLiteDatabase = databaseHelper.getReadableDatabase();
    }
    String count = "SELECT * FROM table";
    Cursor cursor = sqLiteDatabase.rawQuery(count, null);
    if (cursor.moveToFirst()){
        flag = false;
    } else {
        flag = true;
    }
    cursor.close();
    sqLiteDatabase.close();

    return flag;

moveToFirst() check table and return true, if table is empty. Answer that is marked correct - uses extra check.