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