Hari Sankar
Hari Sankar

Reputation: 197

How to check whether table is empty or not in android?

I want to add data to the table called allquestions but have to check whether it is null or it contains any data.When I add data,it gets added every time i run the application.I want to clear the previous data every time I start the application.

db.execSQL("CREATE TABLE IF NOT EXISTS allquestions (num_id INTEGER PRIMARY KEY NOT NULL, questions TEXT NOT NULL,catogery TEXT NOT NULL)" );

How to check whether the table contains any data?I want to add data if it does not contain any data.

Upvotes: 1

Views: 1216

Answers (2)

Axiom
Axiom

Reputation: 88

If you need to do something with the Cursor that gets returned by querying against the database, this is what you can do:

Cursor cursor = db.rawQuery("SELECT * FROM allquestions", null);
if(cursor.moveToFirst()){
 \\Do something with the first row.
} else {
  \\There is nothing in the table.
}

Upvotes: 0

Android Android
Android Android

Reputation: 724

DBHelper dbHelper = new DBHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();        
String query = "SELECT * FROM allquestions";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
cursor.close();
return count

hope it helps

Upvotes: 2

Related Questions