EDIT, Changed the code slightly based on answers below, but still haven\'t got it working. I also added a log message to tell me if getCount was returning > 0, and i
My app was crashing trying the above codes, so I did this and now it's working perfectly!
public boolean checkIfEmpty()
{
Cursor cursor = getDatabase().query(DatabaseHelper.Products.TABLE,
DatabaseHelper.Products.COLUMNS, null, null, null, null, null);
if (cursor != null)
{
try
{
//if it is empty, returns true.
cursor.moveToFirst();
if (cursor.getInt(0) == 0)
return true;
else
return false;
}
//this error usually occurs when it is empty. So i return true as well. :)
catch(CursorIndexOutOfBoundsException e)
{
return true;
}
}
return false;
}
You can check it manually :
Go to DDMS in File Explorer. /data/data/(your application package)/databases
Here you'll get your database file. And if you want to see inserted values in table.
Copy adb
, aapt
, AdbWinApi.dll
and AdbWinUsbApi.dll
files from platform_tools to tools folder.
Then
Open Command Prompt and set your path to "Tools" directory where your android setup exist. It look like as :- (Local Drive):\Android\android-sdk-windows\tools
After set path you'll write command "adb shell" without quotes.
E:\Developers\Android\android-sdk-windows\tools> adb shell
Press Enter and write path of DDMS told above:-
# sqlite3 /data/data/(Your Application Package)/databases/name of database
Press Enter sqlite>.tables
Press Enter And you'll get all table's name existing in that database.
sqlite> Select * from table name
All data exists in that table will show.
For any further query feel free to comment.
Another alternative to the one already mentioned would be to use the function queryNumEntries from de class DatabaseUtils.
An example may be as follows:
public boolean checkEmpty(SQLiteDatabase db, String table){
return DatabaseUtils.queryNumEntries(db, table) == 0;
}
The query SELECT COUNT(*)
on an existing table should never return null. If there are no rows in the table, it should return one row containing the value zero.
Conversely, a row with a non-zero value indicates that it's not empty.
In both cases, one row should be returned, meaning that it will always go through the
//do nothing everything's as it should be
section.
To fix it, leave your query as-is (you don't want to do select column_name
simply because that would be unnecessary and possibly a little inefficient). Leave it as select count(*)
, which will always return one row, and use the following code (tested only in my head so be careful):
Cursor cur = db.rawQuery("SELECT COUNT(*) FROM CAT_BUD_TAB", null);
if (cur != null) {
cur.moveToFirst(); // Always one row returned.
if (cur.getInt (0) == 0) { // Zero count means empty table.
for (int i = 0; i < 13; i++) {
db.execSQL (catInsertArray[i]);
}
}
}
The rawQuery
returns a Cursor
object which is positioned before the first entry (See more info here)
SELECT COUNT(*)
will always return a result (considering the table exists)
So I would do:
if (cur != null){
cur.moveToFirst();
if (cur.getInt(0) == 0) {
// Empty
}
}
My way of checking was something like this:
Cursor cursor = db.query(DbHelper.DB_NAME,DbHelper.DB_C_ID_ONLY,null,null,null,null,null);
if(cursor.isAfterLast())
I get the _id
entries which in my case are auto-increment. If the DB is empty, isAfterLast
for sure returns true.