Skip to content

Commit 897f039

Browse files
committed
compact the welcome memos below 3000 lines: shared pydef demo and WELCOME_END
WELCOME_CREATE now also carries the common pydef examples, and the new WELCOME_END shares the export / transactions / dot-commands / ATTACH tail between both engines (test.db swapped to test.duckdb for DuckDB). Both full memos verified running end-to-end on their engine. 2996 lines.
1 parent 86274c0 commit 897f039

1 file changed

Lines changed: 49 additions & 74 deletions

File tree

sqlite_bro/sqlite_bro.py

Lines changed: 49 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@
5757
INSERT INTO item values('T','Ford',1000);
5858
INSERT INTO item select 'A','Merced',1250 union all select 'W','Wheel',9 ;
5959
INSERT INTO part select ItemNo,'W','needed',Kg/250 from item where Kg>250;
60-
"""
61-
62-
WELCOME_DUCKDB = """-- DuckDB Memo (Demo = click on green "->" icon)
63-
-- (this database uses the DuckDB engine : SQL is stricter than SQLite)
64-
\n-- to CREATE tables : column types are mandatory
65-
""" + WELCOME_CREATE + """\n-- to CREATE a Python embedded function (needs numpy installed),
60+
\n-- to CREATE a Python embedded function,
6661
-- enclose it by "pydef" and ";" :
6762
pydef py_hello():
6863
"hello world"
@@ -71,19 +66,13 @@
7166
"fibonacci : example with function call (may only be internal) "
7267
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
7368
return("%s" % fib(n*1));
74-
pydef py_fib_typed(n: int) -> int:
75-
"type-annotated pydef : registered natively (faster, typed) "
76-
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
77-
return fib(n);
78-
\n-- to USE a python embedded function :
79-
select py_hello(), py_fib(6) as fibonacci, py_fib_typed(7) as fib_typed,
80-
version() as duckdb_version,
81-
current_localtime() t, current_localtimestamp() dt;
82-
\n-- some DuckDB goodies :
83-
DESCRIBE item;
84-
SUMMARIZE item;
85-
select * from duckdb_tables();
86-
\n-- to use COMMIT and ROLLBACK (DuckDB does not support SAVEPOINT) :
69+
"""
70+
71+
WELCOME_END = """\n-- to EXPORT :
72+
-- a TABLE, select TABLE, then click on icon 'SQL->CSV'
73+
-- a QUERY RESULT, select the SCRIPT text, then click on icon '???->CSV',
74+
-- example : select the end of this line: SELECT SQLITE_VERSION()
75+
\n\n-- to use COMMIT and ROLLBACK :
8776
BEGIN TRANSACTION;
8877
UPDATE item SET Kg = Kg + 1;
8978
COMMIT;
@@ -92,19 +81,54 @@
9281
select Kg, Description from Item;
9382
ROLLBACK;
9483
select Kg, Description from Item;
95-
\n-- '.' commands understood (same as in SQLite mode, see sqlite demo) :
84+
85+
\n\n-- '.' commands understood:
86+
-- .backup FILE Backup DB (default "main") to FILE (if Python>=3.7)
87+
-- .cd DIRECTORY Change the working directory to DIRECTORY
88+
-- .dump ?FILE? Render database content as SQL (to FILE if specified)
89+
-- .excel Display the output of next command in spreadsheet
90+
-- .headers on|off Turn display of headers on or off
91+
-- .import FILE TABLE Import data from FILE into TABLE
92+
-- (create TABLE only if it doesn't exist, keep existing records)
93+
-- .once [--bom] FILE Output of next SQL command to FILE [with utf-8 bom]
94+
-- .open ?FILE? Close existing database and reopen FILE
95+
-- .output ?FILE? Send output to FILE or stdout if FILE is omitted
96+
-- .print STRING... Print literal STRING
97+
-- .read FILE Read input from FILE
98+
-- .restore FILE Restore DB (default "main") from FILE (if Python>=3.7)
99+
-- .separator COL Set column separator in next .once exports (default ,)
100+
-- .shell CMD ARGS... Run CMD ARGS... in a system shell
101+
96102
.headers on
97103
.separator ;
98104
.once --bom '~this_file_of result.txt'
99105
select ItemNo, Description from item order by ItemNo desc;
100106
.import '~this_file_of result.txt' in_this_table
101107
.cd ~
102-
ATTACH 'test.duckdb' as toto;
108+
ATTACH 'test.db' as toto;
103109
DROP TABLE IF EXISTS toto.new_item;
104110
CREATE TABLE toto.new_item as select * from "main"."item";
105111
.dump
106112
"""
107113

114+
WELCOME_DUCKDB = """-- DuckDB Memo (Demo = click on green "->" icon)
115+
-- (this database uses the DuckDB engine : SQL is stricter than SQLite)
116+
\n-- to CREATE tables : column types are mandatory
117+
""" + WELCOME_CREATE + """
118+
pydef py_fib_typed(n: int) -> int:
119+
"type-annotated pydef : registered natively (faster, typed) "
120+
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
121+
return fib(n);
122+
\n-- to USE a python embedded function (require numpy installed for DuckDb):
123+
select py_hello(),py_fib_typed(7) as fib_typed, version() as duckdb_version;
124+
125+
\n-- some DuckDB goodies :
126+
DESCRIBE item;
127+
SUMMARIZE item;
128+
select * from duckdb_tables();
129+
130+
""" + WELCOME_END.replace('test.db', 'test.duckdb')
131+
108132

109133
class App:
110134
"""the GUI graphic application"""
@@ -2901,67 +2925,18 @@ def _main():
29012925
welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons)
29022926
-- (tip : open or create a '.duckdb' file to use the DuckDB engine instead)
29032927
\n-- to CREATE a table 'item' and a table 'part' :
2904-
""" + WELCOME_CREATE + """\n-- to CREATE a Python embedded function, enclose them by "py" and ";" :
2905-
pydef py_hello():
2906-
"hello world"
2907-
return ("Hello, World !");
2908-
pydef py_fib(n):
2909-
"fibonacci : example with function call (may only be internal) "
2910-
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
2911-
return("%s" % fib(n*1));
2912-
2913-
-- to USE a python embedded function and nesting of embedded functions:
2928+
""" + WELCOME_CREATE + """
2929+
\n-- to USE a python embedded function :
29142930
select py_hello(), py_fib(6) as fibonacci, sqlite_version(),
29152931
time('now', 'localtime') t, datetime('now', 'localtime') dt;
2916-
\n-- to EXPORT :
2917-
-- a TABLE, select TABLE, then click on icon 'SQL->CSV'
2918-
-- a QUERY RESULT, select the SCRIPT text, then click on icon '???->CSV',
2919-
-- example : select the end of this line: SELECT SQLITE_VERSION()
2920-
\n\n-- to use COMMIT and ROLLBACK :
2921-
BEGIN TRANSACTION;
2922-
UPDATE item SET Kg = Kg + 1;
2923-
COMMIT;
2924-
BEGIN TRANSACTION;
2925-
UPDATE item SET Kg = 0;
2926-
select Kg, Description from Item;
2927-
ROLLBACK;
2928-
select Kg, Description from Item;
2929-
\n\n-- to use SAVEPOINT :
2932+
\n-- to use SAVEPOINT :
29302933
SAVEPOINT remember_Neo; -- create a savepoint
29312934
UPDATE item SET Description = 'Smith'; -- do things
29322935
SELECT ItemNo, Description FROM Item; -- see things done
29332936
ROLLBACK TO SAVEPOINT remember_Neo; -- go back to savepoint state
29342937
SELECT ItemNo, Description FROM Item; -- see all is back to normal
29352938
RELEASE SAVEPOINT remember_Neo; -- free memory
2936-
2937-
\n\n-- '.' commands understood:
2938-
-- .backup FILE Backup DB (default "main") to FILE (if Python>=3.7)
2939-
-- .cd DIRECTORY Change the working directory to DIRECTORY
2940-
-- .dump ?FILE? Render database content as SQL (to FILE if specified)
2941-
-- .excel Display the output of next command in spreadsheet
2942-
-- .headers on|off Turn display of headers on or off
2943-
-- .import FILE TABLE Import data from FILE into TABLE
2944-
-- (create TABLE only if it doesn't exist, keep existing records)
2945-
-- .once [--bom] FILE Output of next SQL command to FILE [with utf-8 bom]
2946-
-- .open ?FILE? Close existing database and reopen FILE
2947-
-- .output ?FILE? Send output to FILE or stdout if FILE is omitted
2948-
-- .print STRING... Print literal STRING
2949-
-- .read FILE Read input from FILE
2950-
-- .restore FILE Restore DB (default "main") from FILE (if Python>=3.7)
2951-
-- .separator COL Set column separator in next .once exports (default ,)
2952-
-- .shell CMD ARGS... Run CMD ARGS... in a system shell
2953-
2954-
.headers on
2955-
.separator ;
2956-
.once --bom '~this_file_of result.txt'
2957-
select ItemNo, Description from item order by ItemNo desc;
2958-
.import '~this_file_of result.txt' in_this_table
2959-
.cd ~
2960-
ATTACH 'test.db' as toto;
2961-
DROP TABLE IF EXISTS toto.new_item;
2962-
CREATE TABLE toto.new_item as select * from "main"."item";
2963-
.dump
2964-
"""
2939+
"""+ WELCOME_END
29652940

29662941
parser = argparse.ArgumentParser(
29672942
description="sqlite_bro : a graphic SQLite and DuckDB browser"

0 commit comments

Comments
 (0)