Skip to content

Instantly share code, notes, and snippets.

@hideshi
Last active October 22, 2023 04:15
Show Gist options
  • Save hideshi/7857224 to your computer and use it in GitHub Desktop.
Save hideshi/7857224 to your computer and use it in GitHub Desktop.
Simple SQLite3 console. You can use DML (SELECT, INSERT, UPDATE, DELETE).
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