fix cursor use

This commit is contained in:
rasbt 2018-04-18 09:47:16 -04:00
parent d93f9fdb9b
commit 5da40c30c0
2 changed files with 58 additions and 49 deletions

View File

@ -682,53 +682,58 @@ convenient script to print a nice overview of SQLite database tables:
import sqlite3
def connect(sqlite_file):
""" Make connection to an SQLite database file """
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
return conn, c
def close(conn):
""" Commit changes and close connection to the database """
# conn.commit()
conn.close()
def total_rows(cursor, table_name, print_out=False):
""" Returns the total number of rows in the database """
c.execute('SELECT COUNT(*) FROM {}'.format(table_name))
count = c.fetchall()
cursor.execute('SELECT COUNT(*) FROM {}'.format(table_name))
count = cursor.fetchall()
if print_out:
print('\nTotal rows: {}'.format(count[0][0]))
return count[0][0]
def table_col_info(cursor, table_name, print_out=False):
"""
Returns a list of tuples with column informations:
(id, name, type, notnull, default_value, primary_key)
""" Returns a list of tuples with column informations:
(id, name, type, notnull, default_value, primary_key)
"""
c.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = c.fetchall()
cursor.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = cursor.fetchall()
if print_out:
print("\nColumn Info:\nID, Name, Type, NotNull, DefaultVal, PrimaryKey")
for col in info:
print(col)
return info
def values_in_col(cursor, table_name, print_out=True):
""" Returns a dictionary with columns as keys and the number of not-null
entries as associated values.
""" Returns a dictionary with columns as keys
and the number of not-null entries as associated values.
"""
c.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = c.fetchall()
cursor.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = cursor.fetchall()
col_dict = dict()
for col in info:
col_dict[col[1]] = 0
for col in col_dict:
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
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
number_rows = len(c.fetchall())
col_dict[col] = number_rows
if print_out:
@ -736,23 +741,22 @@ convenient script to print a nice overview of SQLite database tables:
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)
values_in_col(c, table_name, print_out=True) # slow on large data bases
close(conn)
# next line might be slow on large databases
values_in_col(c, table_name, print_out=True)
Download the script: [print_db_info.py](https://raw.github.com/rasbt/python_sq
lite_code/master/code/print_db_info.py)
close(conn)
Download the script: [print_db_info.py](code/print_db_info.py)
![8_sqlite3_print_db_info_1.png](../../Images/8_sqlite3_print_db_info_1.png)

View File

@ -22,52 +22,57 @@ time: 1
import sqlite3
def connect(sqlite_file):
""" Make connection to an SQLite database file """
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
return conn, c
def close(conn):
""" Commit changes and close connection to the database """
#conn.commit()
# conn.commit()
conn.close()
def total_rows(cursor, table_name, print_out=False):
""" Returns the total number of rows in the database """
c.execute('SELECT COUNT(*) FROM {}'.format(table_name))
count = c.fetchall()
cursor.execute('SELECT COUNT(*) FROM {}'.format(table_name))
count = cursor.fetchall()
if print_out:
print('\nTotal rows: {}'.format(count[0][0]))
return count[0][0]
def table_col_info(cursor, table_name, print_out=False):
"""
Returns a list of tuples with column informations:
(id, name, type, notnull, default_value, primary_key)
""" Returns a list of tuples with column informations:
(id, name, type, notnull, default_value, primary_key)
"""
c.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = c.fetchall()
cursor.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = cursor.fetchall()
if print_out:
print("\nColumn Info:\nID, Name, Type, NotNull, DefaultVal, PrimaryKey")
for col in info:
print(col)
return info
def values_in_col(cursor, table_name, print_out=True):
""" Returns a dictionary with columns as keys and the number of not-null
entries as associated values.
""" Returns a dictionary with columns as keys
and the number of not-null entries as associated values.
"""
c.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = c.fetchall()
cursor.execute('PRAGMA TABLE_INFO({})'.format(table_name))
info = cursor.fetchall()
col_dict = dict()
for col in info:
col_dict[col[1]] = 0
for col in col_dict:
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
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
number_rows = len(c.fetchall())
col_dict[col] = number_rows
if print_out:
@ -85,7 +90,7 @@ if __name__ == '__main__':
conn, c = connect(sqlite_file)
total_rows(c, table_name, print_out=True)
table_col_info(c, table_name, print_out=True)
values_in_col(c, table_name, print_out=True) # slow on large data bases
close(conn)
# next line might be slow on large databases
values_in_col(c, table_name, print_out=True)
close(conn)