Created
January 29, 2016 05:55
-
-
Save amjith/4f61b8a4f3133a5ccdba to your computer and use it in GitHub Desktop.
prompt-toolkit starter template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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