How to print contents of any database file



  1 import sqlite3 as lite
  2 import sys
  3
  4 def print_db_content(database_to_read):
  5         conn = lite.connect(database_to_read);
  6         cur = conn.cursor()
  7         # http://stackoverflow.com/questions/305378/list-of-tables-db-schema-dump-etc-using-the-python-sqlite3-api
  8         # Below query is equivalent to typing .tables in sqlite command line
  9         query = "SELECT name from sqlite_master where type = 'table'"
 10         tables_list = list(cur.execute(query))
 11         print tables_list
 12         for item in tables_list:
 13                 query = "SELECT * from '" + str(item[0]) + "'"
 14                 print query
 15                 cur = conn.cursor()
 16                 data = list(cur.execute(query))
 17                 for item1 in data:
 18                         print item1

 if __name__ == "__main__":
         if (len(sys.argv)<0):
                 print 'Usage: python print_content_of_database.py [database_absolute_path]'               
         else:
                 database=sys.argv[1]
                 print_db_content(database)