Golan Kiviti
Golan Kiviti

Reputation: 4245

Is There a way to know if the database is empty?

Im Working with SQLiteDatabase and i want to create only 1 row on the database, so i wana use if statement for this, my question is if there is a way to know if the database is empty or not. Thank you!

Upvotes: 0

Views: 184

Answers (1)

Simas
Simas

Reputation: 44118

EDIT:

I'll update my answer to help you a little bit further.

If you want to find out if the table exists and if it has rows:

Cursor query = myDB.rawQuery("SELECT count(*) FROM sqlite_master WHERE name = 'table1'", null);
if (query.moveToFirst()) {
    // If that table exists, check if it has any rows
    query = myDB.rawQuery("SELECT count(*) FROM table1", null);
    if (query.moveToFirst()) {
        // Table exists and it has rows. Do something with them here.
    }
}

Upvotes: 1

Related Questions