Skip to content

Instantly share code, notes, and snippets.

@amjith
Created January 29, 2016 05:55
Show Gist options
  • Save amjith/4f61b8a4f3133a5ccdba to your computer and use it in GitHub Desktop.
Save amjith/4f61b8a4f3133a5ccdba to your computer and use it in GitHub Desktop.
prompt-toolkit starter template
import sqlite3
from pygments.lexers.sql import SqlLexer
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
import click
from prompt_toolkit.contrib.completers import WordCompleter
sql_completer = WordCompleter([
'select', 'insert', 'delete', 'drop', 'from', 'where'
])
def evaluate(conn, inp):
with conn:
results = conn.execute(inp).fetchall()
return results
@click.command()
@click.argument('dbname', default=':memory:')
def main(dbname):
history = FileHistory('sqlite-history')
conn = sqlite3.connect(dbname)
while True:
try:
inp = prompt('%s> ' % dbname,
history=history,
lexer=SqlLexer,
completer=sql_completer)
results = evaluate(conn, inp)
print(results)
except EOFError:
print('Good Riddance!')
exit(0)
except Exception as e:
click.secho(str(e), fg='red')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment