2016-04-06 50 views
0

如何僅提取數據庫中表的列名?優化sql輸出

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

使用sqlite_master獲取表名稱列表。然後使用PRAGMA table_info獲取表格中的列。

例子:

PRAGMA table_info(students); 

你需要重複這一過程,你想要的列列出了每個表。