Sqlite Check if Table is Empty

<>

This question already has an answer here:

Answers


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

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;

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

 public boolean isEmpty(String TableName){

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

    if (NoOfRows == 0){
        return true;
    }else {
        return false;
    }
}

Here is a better option:

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

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

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.


Need Your Help

Apache2 warning about NameVirtualHost directive

apache2 ubuntu-10.04

Can someone help me to understand why I receive the following errors when restarting my apache2 webserver:

WPF: Setting the text alignment of a GridViewColumn

wpf xaml listview

By default, text is centered in the headers of a ListView (not in the content cells!), I've been struggling to make it left aligned, here's what I've come up with: