Last active
October 22, 2023 04:15
-
-
Save hideshi/7857224 to your computer and use it in GitHub Desktop.
Simple SQLite3 console.
You can use DML (SELECT, INSERT, UPDATE, DELETE).
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
from cmd import Cmd | |
from sys import argv | |
import sqlite3 | |
import traceback | |
class SqliteCmd(Cmd): | |
dml = ['SELECT', 'INSERT', 'UPDATE', 'DELETE'] | |
def __init__(self, dbfile): | |
super().__init__() | |
self.conn = sqlite3.connect(dbfile) | |
def onecmd(self, arg): | |
flg = False | |
for i in self.dml: | |
if i in arg.upper(): | |
flg = True | |
if not flg: | |
super().onecmd(arg) | |
else: | |
cur = self.conn.cursor() | |
try: | |
cur.execute(arg) | |
if 'SELECT' in arg.upper(): | |
for row in cur: | |
print(row) | |
else: | |
self.conn.commit() | |
except: | |
traceback.print_exc() | |
finally: | |
cur.close | |
def do_quit(self, arg): | |
self.conn.close() | |
quit() | |
return True | |
cmd = SqliteCmd(argv[1]) | |
cmd.prompt = '> ' | |
cmd.cmdloop('This is simple SQLite console.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment