如何通过python使用sqlite3获取表名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34570260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get table names using sqlite3 through python?
提问by krzyhub
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.
我在从 sqlite3 数据库获取数据时遇到问题。我找不到表的名称及其编码。当我通过 sqlitebrowser 打开数据库时,名称只是不可读的字符。与数据库的连接很好。
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?
但是我怎样才能得到表的名称和它们的编码呢?
回答by Kenly
You can use this query to get tables names.
您可以使用此查询来获取表名称。
res = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
for name in res:
print name[0]