如何仅提取数据库中表的列名?
import sqlite3
conn = sqlite3.connect('example3.db')
c = conn.cursor()
c.execute('SELECT name FROM sqlite_master WHERE type=\'table\'') #list all tables therein
print c.fetchall()
c.execute('SELECT sql FROM sqlite_master WHERE type=\'table\' AND name=\'students\'') #list columns in table 'students'
print c.fetchall()
conn.close()
答案 0 :(得分:1)
使用sqlite_master
获取表名列表。然后使用PRAGMA table_info
获取表格中的列。
示例:
PRAGMA table_info(students);
您需要为要列出列表的每个表重复此操作。