Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:35:04+00:00 2026-06-07T17:35:04+00:00

I need a count of the tables that are currently in my sqlite database.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:35:06+00:00Added an answer on June 7, 2026 at 5:35 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables namely: Town; Village; Farmers...and I need to COUNT how many
I need to query the database by joining two tables. Here is what I
I have an sql database. In this database is 3 tables I need to
I need to select the counts from two tables that share a common column
I am trying to join two tables in sql and get the count of
I have currently created a facebook like page that pulls notifications from different tables,
I need to do this query with Java + Hibernate. SELECT table2.id, COUNT(table2.id) AS
I need to count all distinct values from table LDS where status = 'ok'
I have a table and I need to count all unique users on it.
In a table members(id, contractid, a,b,c,d) I need to count: the number of members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.