I have a problem to get data from the sqlite3 database. I can't find out the names of tables and their encoding. When I open DB through sqlitebrowser names were just unreadable characters. Connection to DB is fine.
conn = sqlite3.connect('my.db')
conn_cursor = conn.cursor()
conn.text_factory = str
But how can I get the names of tables and their encoding?
To list all tables in an SQLite3 database, you should query the sqlite_master table and then use the fetchall() to fetch the results from the SELECT statement. The sqlite_master is the master table in SQLite3, which stores all tables.
So to get a list of all tables in the database, use the following SELECT command: SELECT name FROM sqlite_schema WHERE type='table' ORDER BY name; For indices, type is equal to 'index', name is the name of the index and tbl_name is the name of the table to which the index belongs.
SQLite Python: Querying Data First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.
You can use this query to get tables names.
res = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
for name in res.fetchall():
print(name[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With