Guys, tell me, I have a database 'data_per.db', it has 3 tables users, product, recipe, how can I check if the database is empty or not, I mean, have these tables been created?
I am using sqlite I know that this is how the number in the users table is returned:

"SELECT COUNT (*) FROM users;"

And how exactly to check the tables in the database file itself?

you can create a table, if it already exists, then sqlite will return an IntegrityError

Clark Devlin2021-11-24 07:46:03

@ClarkDevlin can create tables with one SQL query if they do not exist. But the question is posed differently.

Михаил Алексеевич2021-11-24 09:48:50
  • Answer # 1

    Like this.

    import sqlite3
    with sqlite3.connect ('data_per.db') as con:
        tables= ["'users'", "'product'", "'recipe'"]
        table_names= ','. join (tables)
        SQL= f "SELECT count (*) FROM sqlite_master WHERE type= 'table' AND name in ({table_names});"
        num= con.execute (SQL) .fetchone () [0]
        if num!= len (tables):
            print ('Not all tables are there')
            print ('All tables are there')