Skip to content

Instantly share code, notes, and snippets.

@tyabu12
Created January 26, 2020 06:01
Show Gist options
  • Save tyabu12/ec5ed23d9c21ce7e9aeeb9639c00dc42 to your computer and use it in GitHub Desktop.
Save tyabu12/ec5ed23d9c21ce7e9aeeb9639c00dc42 to your computer and use it in GitHub Desktop.
import readline
import sqlite3
def print_row(row):
print('(' + ', '.join(str(_) for _ in row) + ')')
conn = sqlite3.connect(':memory:')
c = conn.cursor()
while True:
q = ''
line = raw_input('query> ')
if line.strip() == 'exit':
break
while True:
q += line
if line[-1:] == ';':
break
line = raw_input('')
q = q.strip()
print('Execute: ' + q)
try:
c.execute(q)
for i, row in enumerate(c):
if i == 0:
print_row([desc[0] for desc in c.description])
print_row(list(row))
except sqlite3.Error as e:
print(e)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment