@@ -682,77 +682,81 @@ convenient script to print a nice overview of SQLite database tables:
682
682
683
683
684
684
import sqlite3
685
-
685
+
686
+
686
687
def connect(sqlite_file):
687
688
""" Make connection to an SQLite database file """
688
689
conn = sqlite3.connect(sqlite_file)
689
690
c = conn.cursor()
690
691
return conn, c
691
-
692
+
693
+
692
694
def close(conn):
693
695
""" Commit changes and close connection to the database """
694
696
# conn.commit()
695
697
conn.close()
696
-
698
+
699
+
697
700
def total_rows(cursor, table_name, print_out=False):
698
701
""" 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()
701
704
if print_out:
702
705
print('\nTotal rows: {}'.format(count[0][0]))
703
706
return count[0][0]
704
-
707
+
708
+
705
709
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)
710
712
"""
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
+
714
716
if print_out:
715
717
print("\nColumn Info:\nID, Name, Type, NotNull, DefaultVal, PrimaryKey")
716
718
for col in info:
717
719
print(col)
718
720
return info
719
-
721
+
722
+
720
723
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.
723
726
"""
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()
726
729
col_dict = dict()
727
730
for col in info:
728
731
col_dict[col[1]] = 0
729
732
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
732
737
number_rows = len(c.fetchall())
733
738
col_dict[col] = number_rows
734
739
if print_out:
735
740
print("\nNumber of entries per column:")
736
741
for i in col_dict.items():
737
742
print('{}: {}'.format(i[0], i[1]))
738
743
return col_dict
739
-
740
-
744
+
745
+
741
746
if __name__ == '__main__':
742
-
747
+
743
748
sqlite_file = 'my_first_db.sqlite'
744
749
table_name = 'my_table_3'
745
-
750
+
746
751
conn, c = connect(sqlite_file)
747
752
total_rows(c, table_name, print_out=True)
748
753
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
+
751
757
close(conn)
752
-
753
758
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 )
756
760
757
761
![ 8_sqlite3_print_db_info_1.png] ( ../../Images/8_sqlite3_print_db_info_1.png )
758
762
0 commit comments