优化sql输出

时间:2016-04-06 19:06:04

标签: sql sqlite output

如何仅提取数据库中表的列名?

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()

1 个答案:

答案 0 :(得分:1)

使用sqlite_master获取表名列表。然后使用PRAGMA table_info获取表格中的列。

示例:

PRAGMA table_info(students);

您需要为要列出列表的每个表重复此操作。