I need a count of the tables that are currently in my sqlite database. Tried searching but did not get any direct method.
I tried this method in my DbHelper.
public int countTables() {
int count = 0;
String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
Cursor cursor = getReadableDatabase()
.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
count++;
getReadableDatabase().close();
}
cursor.close();
return count;
}
But this method gives me a wrong count. I have only 3 tables in my DB, but this method returns count as 5. What could be wrong with this method. Is there any direct method to get the count.
Syntax of SQLite Count() Function Expression – Its column or expression which we used to count number of non-NULL values available in the table. Tablename – Its name of the table which we wish to retrieve records from.
Maximum Number Of Tables In A Join SQLite does not support joins containing more than 64 tables. This limit arises from the fact that the SQLite code generator uses bitmaps with one bit per join-table in the query optimizer.
To check the count of tables. mysql> SELECT count(*) AS TOTALNUMBEROFTABLES -> FROM INFORMATION_SCHEMA. TABLES -> WHERE TABLE_SCHEMA = 'business'; The following output gives the count of all the tables.
If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.
Your method is fine, you could even use SELECT count(*) ...
. But there are two tables created automatically android_metadata
and sqlite_sequence
, simply takes these into account: 5 - 2 = 3 tables that you created.
Or since Squonk provided excellent documentation about when sqlite_sequence
is created and when it might not exist, I recommend this query:
SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name != 'android_metadata' AND name != 'sqlite_sequence';
A few pointers to shorten your code
All of this:
cursor.moveToFirst();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Can be accomplished like this:
while(cursor.moveToNext()) {
Cursor.moveToNext() return true if the next row exists. If a new cursor is not empty moveToNext() will return true and set this index on the first position. When cursor.isAfterLast() == true
, moveToNext() returns false.
But if you simply want to count how many rows are in the Cursor use:
int count = cursor.getCount();
Lastly, store your writable database in a variable.
getReadableDatabase().close();
I haven't tested this, but if getReadableDatabase() returns a new SQLiteDatabase object each time then you are simply closing this new database as soon as you have created it... I would close the database once and I would close it after I have closed all of the Cursors I retrieved from it.
SQLiteDatabase database = getReadableDatabase();
...
database.close();
If you are using a lot of queries consider opening your database in onResume() and closing it in onPause().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With