Is there a pythonistic way available to search all the tables and columns of an Sqlite3 database? The only inputs would be the name of the sqlite DB file or a raw input.

I am trying to write a forensics tool and want to search sqlite files for a specific string.

This works for me with sqlalchemy

from sqlalchemy import create_engine
from sqlalchemy.engine import reflection
engine = create_engine('sqlite:///menus.sqlite') # //// for abs path
insp = reflection.Inspector.from_engine(engine)
print insp.get_table_names() # print list of table names
print insp.get_columns(u'menu') # print list of dicts, each containing info about a column in table 'menu'

reflection
connect strings

Here is another way without sqlalchemy

import sqlite3
conn = sqlite3.connect('menus.sqlite')
cur = conn.cursor()
s = "PRAGMA table_info(`menu`)"
try:
    cur.execute(s)
    print(cur.fetchall())
finally:
    cur.close()
    conn.close()

See table_info and also try the pragma in sqliteman to see the result.

Tables are rows of type 'table' in table sqlite_master.

See also this code snippet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.