16

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?

2
  • 2
    The database's text encoding is stored as a 4-byte big-endian int at offset 56 in the sqlite database header: header = open('my.db').read(100); import struct; encoding_type = struct.unpack('>i', header[56:60])[0]. The encoding_type is equal to either 1, 2, or 3 and corresponds to UTF-8, UTF-16LE, or UTF-16BE respectively. Commented Jan 2, 2016 at 20:49
  • 1
    Opening an SQLite database manually is a good way to get a corrupted database. Use the encoding pragma instead. Commented Jan 3, 2016 at 5:53

1 Answer 1

38

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])
Sign up to request clarification or add additional context in comments.

4 Comments

Okay. Then to print the value should I call on it fetchall? But it returns just an empty list []
@krzyhub Check my edit.
Nothing was printed, but it seems that this is for some reason my unaware mistake somewhere else. But thanks.
@krzyhub For new created db, we need to commit before.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.