Android Question Count of tables in SQL database.

Discussion in 'Android Questions' started by Inuma, Nov 9, 2018.

  1. Inuma

    Inuma New Member Licensed User

    I have a sql database with an unknown count of tables. How to find the number of tables in the database?
    For example I try:

    dim count_tables as Cursor
    count_tables= Main.sql1.ExecQuery(
    "SELECT COUNT() FROM sqlite_master WHERE TYPE='table'")
    but it returns error "column not exists"
  2. Erel

    Erel Administrator Staff Member Licensed User

    Correct code:
    Dim count_tables As ResultSet = sql.ExecQuery("SELECT COUNT() FROM sqlite_master WHERE TYPE='table'")
    Log("Number of tables: " & count_tables.GetInt2(0))
  3. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    This is a bit simpler:

    Sub CountTables
    Dim lCountTables As Long = SQL.ExecQuerySingleResult("select count(*) from sqlite_master where type = 'table'")
    Log("CountTables, lCountTables: " & lCountTables)
    End Sub
  4. Inuma

    Inuma New Member Licensed User

    Thanks. It's working
  5. eps

    eps Well-Known Member Licensed User

    I would put the number in to an Int, but Long would more than suffice. There's no real need to put the asterisk in, I'd prefer a column_name in there, but it could throw an error if it no longer exists. It used to be accepted SQL practice, but then was frowned upon, but might work fine in this scenario but not in a 'normal' counting scenario, e.g. number of records in a specific table.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice