@@ -682,77 +682,81 @@ convenient script to print a nice overview of SQLite database tables:
682682
683683
684684 import sqlite3
685-
685+
686+
686687 def connect(sqlite_file):
687688 """ Make connection to an SQLite database file """
688689 conn = sqlite3.connect(sqlite_file)
689690 c = conn.cursor()
690691 return conn, c
691-
692+
693+
692694 def close(conn):
693695 """ Commit changes and close connection to the database """
694696 # conn.commit()
695697 conn.close()
696-
698+
699+
697700 def total_rows(cursor, table_name, print_out=False):
698701 """ Returns the total number of rows in the database """
699- c .execute('SELECT COUNT(*) FROM {}'.format(table_name))
700- count = c .fetchall()
702+ cursor .execute('SELECT COUNT(*) FROM {}'.format(table_name))
703+ count = cursor .fetchall()
701704 if print_out:
702705 print('\nTotal rows: {}'.format(count[0][0]))
703706 return count[0][0]
704-
707+
708+
705709 def table_col_info(cursor, table_name, print_out=False):
706- """
707- Returns a list of tuples with column informations:
708- (id, name, type, notnull, default_value, primary_key)
709-
710+ """ Returns a list of tuples with column informations:
711+ (id, name, type, notnull, default_value, primary_key)
710712 """
711- c .execute('PRAGMA TABLE_INFO({})'.format(table_name))
712- info = c .fetchall()
713-
713+ cursor .execute('PRAGMA TABLE_INFO({})'.format(table_name))
714+ info = cursor .fetchall()
715+
714716 if print_out:
715717 print("\nColumn Info:\nID, Name, Type, NotNull, DefaultVal, PrimaryKey")
716718 for col in info:
717719 print(col)
718720 return info
719-
721+
722+
720723 def values_in_col(cursor, table_name, print_out=True):
721- """ Returns a dictionary with columns as keys and the number of not-null
722- entries as associated values.
724+ """ Returns a dictionary with columns as keys
725+ and the number of not-null entries as associated values.
723726 """
724- c .execute('PRAGMA TABLE_INFO({})'.format(table_name))
725- info = c .fetchall()
727+ cursor .execute('PRAGMA TABLE_INFO({})'.format(table_name))
728+ info = cursor .fetchall()
726729 col_dict = dict()
727730 for col in info:
728731 col_dict[col[1]] = 0
729732 for col in col_dict:
730- c.execute('SELECT ({0}) FROM {1} WHERE {0} IS NOT NULL'.format(col, table_name))
731- # In my case this approach resulted in a better performance than using COUNT
733+ c.execute('SELECT ({0}) FROM {1} '
734+ 'WHERE {0} IS NOT NULL'.format(col, table_name))
735+ # In my case this approach resulted in a
736+ # better performance than using COUNT
732737 number_rows = len(c.fetchall())
733738 col_dict[col] = number_rows
734739 if print_out:
735740 print("\nNumber of entries per column:")
736741 for i in col_dict.items():
737742 print('{}: {}'.format(i[0], i[1]))
738743 return col_dict
739-
740-
744+
745+
741746 if __name__ == '__main__':
742-
747+
743748 sqlite_file = 'my_first_db.sqlite'
744749 table_name = 'my_table_3'
745-
750+
746751 conn, c = connect(sqlite_file)
747752 total_rows(c, table_name, print_out=True)
748753 table_col_info(c, table_name, print_out=True)
749- values_in_col(c, table_name, print_out=True) # slow on large data bases
750-
754+ # next line might be slow on large databases
755+ values_in_col(c, table_name, print_out=True)
756+
751757 close(conn)
752-
753758
754- Download the script: [ print_db_info.py] (https://raw.github.com/rasbt/python_sq
755- lite_code/master/code/print_db_info.py)
759+ Download the script: [ print_db_info.py] ( code/print_db_info.py )
756760
757761![ 8_sqlite3_print_db_info_1.png] ( ../../Images/8_sqlite3_print_db_info_1.png )
758762
0 commit comments