python_reference/tutorials/sqlite3_howto/code/print_db_info.py

97 lines
2.5 KiB
Python
Raw Normal View History

2014-04-30 02:49:59 +00:00
# Sebastian Raschka 2014
# Prints Information of a SQLite database.
# E.g.,
#
"""
Total rows: 1
Column Info:
ID, Name, Type, NotNull, DefaultVal, PrimaryKey
(0, 'id', 'TEXT', 0, None, 1)
(1, 'date', '', 0, None, 0)
(2, 'time', '', 0, None, 0)
(3, 'date_time', '', 0, None, 0)
Number of entries per column:
date: 1
date_time: 1
id: 1
time: 1
"""
import sqlite3
2018-04-18 13:47:16 +00:00
2014-04-30 02:49:59 +00:00
def connect(sqlite_file):
""" Make connection to an SQLite database file """
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
return conn, c
2018-04-18 13:47:16 +00:00
2014-04-30 02:49:59 +00:00
def close(conn):
""" Commit changes and close connection to the database """
2018-04-18 13:47:16 +00:00
# conn.commit()
2014-04-30 02:49:59 +00:00
conn.close()
2018-04-18 13:47:16 +00:00
2014-04-30 02:49:59 +00:00
def total_rows(cursor, table_name, print_out=False):
""" Returns the total number of rows in the database """
2018-04-18 13:47:16 +00:00
cursor.execute('SELECT COUNT(*) FROM {}'.format(table_name))
count = cursor.fetchall()
2014-04-30 02:49:59 +00:00
if print_out:
print('\nTotal rows: {}'.format(count[0][0]))
return count[0][0]
2018-04-18 13:47:16 +00:00
2014-04-30 02:49:59 +00:00
def table_col_info(cursor, table_name, print_out=False):
2018-04-18 13:47:16 +00:00
""" Returns a list of tuples with column informations:
(id, name, type, notnull, default_value, primary_key)
2014-04-30 02:49:59 +00:00
"""
2018-04-18 13:47:16 +00:00
cursor.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = cursor.fetchall()
2014-04-30 02:49:59 +00:00
if print_out:
print("\nColumn Info:\nID, Name, Type, NotNull, DefaultVal, PrimaryKey")
for col in info:
print(col)
return info
2018-04-18 13:47:16 +00:00
2014-04-30 02:49:59 +00:00
def values_in_col(cursor, table_name, print_out=True):
2018-04-18 13:47:16 +00:00
""" Returns a dictionary with columns as keys
and the number of not-null entries as associated values.
2014-04-30 02:49:59 +00:00
"""
2018-04-18 13:47:16 +00:00
cursor.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = cursor.fetchall()
2014-04-30 02:49:59 +00:00
col_dict = dict()
for col in info:
col_dict[col[1]] = 0
for col in col_dict:
2018-04-18 13:47:16 +00:00
c.execute('SELECT ({0}) FROM {1} '
'WHERE {0} IS NOT NULL'.format(col, table_name))
# In my case this approach resulted in a
# better performance than using COUNT
2014-04-30 02:49:59 +00:00
number_rows = len(c.fetchall())
col_dict[col] = number_rows
if print_out:
print("\nNumber of entries per column:")
for i in col_dict.items():
print('{}: {}'.format(i[0], i[1]))
return col_dict
if __name__ == '__main__':
sqlite_file = 'my_first_db.sqlite'
table_name = 'my_table_3'
conn, c = connect(sqlite_file)
total_rows(c, table_name, print_out=True)
table_col_info(c, table_name, print_out=True)
2018-04-18 13:47:16 +00:00
# next line might be slow on large databases
values_in_col(c, table_name, print_out=True)
2014-04-30 02:49:59 +00:00
2018-04-18 13:47:16 +00:00
close(conn)