Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of tables in the sqlite database

Tags:

android

sqlite

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.

like image 684
darsh Avatar asked Jul 16 '12 14:07

darsh


People also ask

How do I find the number of tables in SQLite?

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.

How many tables are there in SQLite database?

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.

How do you find the number of tables in a database?

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.

How do I get a list of tables in SQLite database?

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.


1 Answers

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().

like image 143
Sam Avatar answered Oct 17 '22 08:10

Sam