Skip to content

Commit 23a2daa

Browse files
committed
make my scripting day
not everybody has the multiple choice between sqlite.exe and sqlite.dll
1 parent 516b9e7 commit 23a2daa

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Changelog
1111

1212
* supports '.cd DIRECTORY' function
1313

14+
* supports command-line scripting (see sqlite_bro -h)
15+
1416

1517
2021-04-25a : v0.9.3 'Hello, World!'
1618
------------------------------------

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Features
1818

1919
* Support of sql-embedded Python functions
2020

21+
* support supports command-line scripting (see sqlite_bro -h), with or without Graphic User Interface
22+
2123
* Easy to distribute : 1 Python source file, Python and PyPy3 compatible
2224

2325
* Easy to start : just launch sqlite_bro
@@ -45,6 +47,10 @@ Example usage
4547
::
4648

4749
$ sqlite_bro
50+
51+
::
52+
53+
$ sqlite_bro -h
4854
4955
Screenshot
5056
----------

sqlite_bro/sqlite_bro.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
from __future__ import print_function, unicode_literals, division # Python2.7
4-
4+
import argparse
55

66
import sqlite3 as sqlite
77
import sys
@@ -33,7 +33,7 @@ class App:
3333
def __init__(self):
3434
"""create a tkk graphic interface with a main window tk_win"""
3535
self.__version__ = '0.10.0'
36-
self._title = "of 2021-08-01a : 'Hello, better .scripting World!'"
36+
self._title = "of 2021-08-01a : 'Hello, scripting World!'"
3737
self.conn = None # Baresql database object
3838
self.database_file = ""
3939
self.tk_win = Tk()
@@ -1598,10 +1598,33 @@ def export_writer(self, sql, csv_file, header=True,
15981598
writer.writerows(cursor.fetchall())
15991599

16001600
def _main():
1601+
parser = argparse.ArgumentParser(description='sqlite_bro : a graphic SQLite browser in 1 Python file')
1602+
parser.add_argument("-q", "--quiet", action='store_true', help="do not launch the gui")
1603+
parser.add_argument("-w", "--wait", action='store_true', help="wait the user to launch the scripts")
1604+
parser.add_argument("-db", "--database", default=":memory:", type=str, help="specify initial Database if not ':memory:'")
1605+
parser.add_argument("-sc", "--scripts", type=str, help="qive a list of initial scripts")
1606+
args = parser.parse_args()
1607+
print(args)
16011608
app = App()
16021609
# start with a memory Database and a welcome
16031610
app.new_db(":memory:")
1604-
welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons)
1611+
1612+
if args.database:
1613+
app.open_db(args.database)
1614+
if args.scripts:
1615+
if isinstance(args.scripts, str):
1616+
scripts = [args.scripts, '', '']
1617+
else:
1618+
scripts = args.scripts
1619+
for script in scripts:
1620+
if os.path.isfile(script):
1621+
with io.open(script, encoding=guess_encoding(script)[0]) as f:
1622+
welcome_text = f.read()
1623+
app.n.new_query_tab("Welcome", welcome_text)
1624+
if args.wait == False:
1625+
app.run_tab()
1626+
else:
1627+
welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons)
16051628
\n-- to CREATE a table 'items' and a table 'parts' :
16061629
DROP TABLE IF EXISTS item; DROP TABLE IF EXISTS part;
16071630
CREATE TABLE item (ItemNo, Description,Kg , PRIMARY KEY (ItemNo));
@@ -1672,8 +1695,11 @@ def _main():
16721695
DROP TABLE IF EXISTS toto.new_item;
16731696
CREATE TABLE toto.new_item as select * from "main"."item"
16741697
"""
1675-
app.n.new_query_tab("Welcome", welcome_text)
1676-
app.tk_win.mainloop()
1698+
app.n.new_query_tab("Welcome", welcome_text)
1699+
if args.quiet == True:
1700+
app.close_db
1701+
else:
1702+
app.tk_win.mainloop()
16771703

16781704
if __name__ == '__main__':
16791705
_main() # create a tkk graphic interface with a main window tk_win

0 commit comments

Comments
 (0)