Comments (1)
Here's an initial prototype with transaction support, which I have not yet tested properly:
import click
import readline
import sqlite_utils
from sqlite_utils.utils import sqlite3
import tabulate
SQL_KEYWORDS = ['select ', 'from ', 'where ', 'insert ', 'update ', 'delete ', 'create ', 'drop ', 'begin ', 'commit ', 'rollback ']
def completer(text, state):
options = [i for i in SQL_KEYWORDS if i.lower().startswith(text.lower())]
if state < len(options):
return options[state]
else:
return None
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
readline.set_completer(completer)
@sqlite_utils.hookimpl
def register_commands(cli):
@cli.command()
@click.argument("path", type=click.Path(dir_okay=False, readable=True))
def shell(path):
"Start an interactive SQL shell for this database"
run_sql_shell(path)
def run_sql_shell(path):
db = sqlite_utils.Database(path)
cursor = db.conn.cursor()
print("Enter your SQL commands to execute in sqlite3.")
print("Type 'exit' to exit.")
print("Type 'BEGIN' to start a transaction, and 'COMMIT' or 'ROLLBACK' to end it.")
statement = ""
in_transaction = False
prompt = 'sqlite-utils> '
while True:
line = input(prompt)
if line:
readline.add_history(line)
prompt = ' ...> '
if line.lower() in ("exit", "quit"):
break
if line.lower() == "begin":
in_transaction = True
if in_transaction and line.lower() in ["commit", "rollback"]:
in_transaction = False
statement += ' ' + line
if sqlite3.complete_statement(statement):
try:
statement = statement.strip()
cursor.execute(statement)
if statement.lower().startswith("select"):
print(cursor.fetchall())
elif not in_transaction:
db.conn.commit()
except sqlite3.Error as e:
print("An error occurred:", e)
if in_transaction:
print("Transaction has been rolled back.")
db.conn.rollback()
finally:
prompt = 'sqlite-utils> '
statement = ""
db.conn.close()
from sqlite-utils-shell.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlite-utils-shell.