How to list tables in SQLite3 database in Python
Also see How to list SQLite3 database tables on command line
You can use this snippet to list all the SQL tables in your SQLite 3.x database in Python:
def tables_in_sqlite_db(conn):
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [
v[0] for v in cursor.fetchall()
if v[0] != "sqlite_sequence"
]
cursor.close()
return tables
Usage example:
#!/usr/bin/env python3
import sqlite3
# Open database
conn = sqlite3.connect('/usr/share/command-not-found/commands.db')
# List tables
tables = tables_in_sqlite_db(conn)
# Your code goes here!
# Example:
print(tables) # prints ['commands', 'packages']
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow