mercredi 6 avril 2016

optimize sql output

How to extract only the column names for tables in a database?

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

Aucun commentaire:

Enregistrer un commentaire