Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get table names using sqlite3 through python? [duplicate]

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?

like image 907
krzyhub Avatar asked Jan 02 '16 20:01

krzyhub


People also ask

How do I get a list of tables in SQLite database in Python?

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.

How do I find the table name in SQLite?

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.

How fetch data from sqlite3 in Python?

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.


1 Answers

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])
like image 172
Kenly Avatar answered Sep 23 '22 18:09

Kenly