Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Last active December 8, 2021 23:56
Show Gist options
  • Save psychemedia/14ac489ebb6bec3ee84f to your computer and use it in GitHub Desktop.
Save psychemedia/14ac489ebb6bec3ee84f to your computer and use it in GitHub Desktop.
M269 SQL
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import sqlite3
m269_database="./m269_database.db"
def query(q):
"""Applies an sql query to the M269 database"""
connection=sqlite3.connect(m269_database)
cursor=connection.cursor()
cursor.execute(q)
u=cursor.fetchall()
cursor.close()
return u
def show(q):
"""Applies an sql query to the M269 database and prints the result"""
connection=sqlite3.connect(m269_database)
cursor=connection.cursor()
cursor.execute(q)
columnNames=[n[0] for n in cursor.description]
rows=cursor.fetchall()
numberOfColumns=len(columnNames)
cursor.close()
# For each column, get the width of the widest element (including
# the header)
columnWidths=[]
for cn in range(numberOfColumns):
if rows==[]:
columnWidths.append(len(columnNames[cn])+2)
else:
maxWidth=max(len(columnNames[cn]),
max([len(str(cw[cn])) for cw in rows]))
columnWidths.append(maxWidth+2)
# Now print the columns
print()
# Headers
print('|'.join([(' '+columnNames[i]).ljust(columnWidths[i])
for i in range(numberOfColumns)]))
# Separators
print('|'.join(['-'*columnWidths[i] for i in range(numberOfColumns)]))
# rows
for row in rows:
print('|'.join([(' '+str(row[i])).ljust(columnWidths[i])
for i in range(numberOfColumns)]))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment