Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active July 18, 2018 03:35
Show Gist options
  • Select an option

  • Save BlogBlocks/453817a53d4f6737f53e55fab9b25a14 to your computer and use it in GitHub Desktop.

Select an option

Save BlogBlocks/453817a53d4f6737f53e55fab9b25a14 to your computer and use it in GitHub Desktop.
"""
Gets tablename and column names from unknown database
just enter database name.
USAGE:
import dbINFO
database= "databases/history.db"
#database= "databases/sat2.db"
#database= "databases/ImageD.db"
dbINFO.dbinfo(database)
"""
import sqlite3
def dbinfo(database):
conn = sqlite3.connect(database)
conn.text_factory = str
c = conn.cursor()
res = c.execute("SELECT name FROM sqlite_master WHERE type='table';")
row = c.fetchone()
row = str(row)
row = row.replace("(","");row = row.replace(",)","")
row = row.replace("'","")
print row
cur = c.execute("select * from \"%s\"" % row)
columns = [description[0] for description in cur.description]
return columns
# if run directly
#database = "databases/history.db"
#dbinfo(database)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment