Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite Check if Table is Empty [duplicate]

Well, I have a databse and it has lots of table. but generally tables are empty. I want check if a database table is empty. IF table is empty, program will fill it.

public static long queryNumEntries (SQLiteDatabase db, String table) 

I will use it but it requre API 11.

like image 831
mehmet Avatar asked Mar 25 '14 09:03

mehmet


People also ask

How to remove duplicates in SQLite?

The DISTINCT clause allows you to remove the duplicate rows in the result set. In this syntax: First, the DISTINCT clause must appear immediately after the SELECT keyword. Second, you place a column or a list of columns after the DISTINCT keyword.

Is null or empty in SQLite?

SQLite IS NOT NULL operator.

How do I check if a Python database is empty?

Show activity on this post. import MySQLdb db = MySQLdb. connect(passwd="moonpie", db="thangs") results = db. query("""SELECT * from mytable limit 1""") if not results: print "This table is empty!"


2 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 
like image 186
Waqar Ahmed Avatar answered Sep 19 '22 14:09

Waqar Ahmed


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; 
like image 22
Luca Sepe Avatar answered Sep 20 '22 14:09

Luca Sepe