AJW
AJW

Reputation: 1649

Android: after SQLite database has data that is deleted how do I confirm database is empty?

I am new to SQLite and am struggling to test for database being empty. I have a RecyclerView list populated with CardViews. The data for each CardView comes from a user input UI that updates a SQLite database. The input is a simple "Name" EditText. So if the user inputs two Names, then I will have two CardViews in the RecyclerView list and two rows in the database. If the user then deletes both CardViews, the database should be updated and will then be empty.

How do I check that the database is empty? I need a row count of zero or a similar indicator that shows that the database is empty. The count at zero or the indicator at zero will then allow me to switch to a different layout for the ReyclerView list as I need to update the list to show a default layout of zero CardViews since the user has deleted both CardViews by virtue of deleting the two Names from the original two rows in the database. Please advise.

Upvotes: 0

Views: 1008

Answers (3)

Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
Boolean isdbempty= mCursor.getCount()>0 ? false:true;
mCursor.close();

Upvotes: 1

Sasi Kumar
Sasi Kumar

Reputation: 13348

Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
Boolean isdbempty;
if (mCursor.moveToFirst())
 {   
 isdbempty= true;
 } else
{  
isdbempty= false;
}

Upvotes: 1

Joker
Joker

Reputation: 545

Check if Table is empty or not.Take count of table rows.

public int getProfilesCount() { 
String countQuery = "SELECT  * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;} 

Upvotes: 1

Related Questions