@@ -40,8 +40,8 @@ class App:
4040
4141 def __init__ (self , use_gui = True ):
4242 """create a tkk graphic interface with a main window tk_win"""
43- self .__version__ = "0.12.0 "
44- self ._title = "of 2021-08-14a : 'Pop-up result to .excel !'"
43+ self .__version__ = "0.12.1 "
44+ self ._title = "of 2021-08-15a : 'Pop-up results to .excel !'"
4545 self .conn = None # Baresql database object
4646 self .database_file = ""
4747 self .initialdir = "."
@@ -137,10 +137,12 @@ def create_menu(self):
137137 self .menu .add_command (label = "Close Database" , command = self .close_db )
138138 self .menu .add_separator ()
139139 self .menu .add_command (label = "Attach Database" , command = self .attach_db )
140- if sys .version_info [:2 ] >= (3 , 7 ):
140+ if sys .version_info [:2 ] >= (3 , 7 ):
141141 self .menu .add_separator ()
142- self .menu .add_command (label = "Backup current Database" , command = self .backup_db )
143- self .menu .add_command (label = "Restore into current Database" , command = self .restore_db )
142+ self .menu .add_command (label = "Backup main Database" , command = self .backup_db )
143+ self .menu .add_command (
144+ label = "Restore into main Database" , command = self .restore_db
145+ )
144146 self .menu .add_separator ()
145147 self .menu .add_command (label = "Quit" , command = self .quit_db )
146148
@@ -190,6 +192,12 @@ def create_toolbar(self):
190192 ("sqlin_img" , self .load_script , "Load a SQL script file" ),
191193 ("sqlsav_img" , self .sav_script , "Save a SQL script in a file" ),
192194 ("chgsz_img" , self .chg_fontsize , "Modify font size" ),
195+ (
196+ "img_run_temp" ,
197+ self .run_temp ,
198+ "Run script selection and Display output in temporary files" ,
199+ ),
200+ ("img_clean_temp" , self .clean_temp , "Remove old temporary files" ),
193201 ]
194202
195203 for img , action , tip in to_show :
@@ -467,6 +475,41 @@ def exsav_script(self):
467475 self .create_and_add_results (script , active_tab_id , limit = 99 , log = f )
468476 fw .focus_set () # workaround bug http://bugs.python.org/issue17511
469477
478+ def run_temp (self ):
479+ """run selected script commands and display results via tmp files"""
480+ # backup existing defaults
481+ once_mode_bkp = self .once_mode
482+ encode_in_bkp = self .encode_in
483+ output_file_bkp = self .output_file
484+ init_output_bkp = self .init_output
485+ output_mode_bkp = self .output_mode
486+ x_mode_bkp = self .x_mode
487+
488+ active_tab_id = self .n .notebook .select ()
489+ if active_tab_id != "" :
490+ self .n .remove_treeviews (active_tab_id )
491+
492+ if active_tab_id != "" :
493+ # get current selection (or all)
494+ fw = self .n .fw_labels [active_tab_id ]
495+ fw = self .n .fw_labels [active_tab_id ]
496+ try :
497+ query = fw .get ("sel.first" , "sel.last" )
498+ except :
499+ query = fw .get (1.0 , END )[:- 1 ]
500+ query = "\n .output --bom -x \n " + query
501+ # print("run temp", query)
502+ self .create_and_add_results (query , active_tab_id )
503+ fw .focus_set () # workaround bug http://bugs.python.org/issue17511
504+
505+ # restore previous defaults
506+ self .once_mode = once_mode_bkp
507+ self .encode_in = encode_in_bkp
508+ self .output_file = output_file_bkp
509+ self .init_output = init_output_bkp
510+ self .output_mode = output_mode_bkp
511+ self .x_mode = x_mode_bkp
512+
470513 def chg_fontsize (self ):
471514 """change the display font size"""
472515 sizes = [10 , 13 , 14 ]
@@ -496,6 +539,37 @@ def chg_fontsize(self):
496539 size = self .font_size , weight = ww [self .font_wheight ], family = ff
497540 )
498541
542+ def clean_temp (self ):
543+ """clear temp directory"""
544+ ff = tmpf .NamedTemporaryFile (delete = True , suffix = "_sqlite_bro.csv" ).name
545+ temp_directory = os .path .dirname (ff )
546+ report = [
547+ ("" , "" ),
548+ ]
549+ for file in os .listdir (temp_directory ):
550+ if file .endswith ("_sqlite_bro.csv" ):
551+ print ("removing " , os .path .join (temp_directory , file ))
552+ try :
553+ os .remove (os .path .join (temp_directory , file ))
554+ report += [
555+ ("removing " , os .path .join (temp_directory , file )),
556+ ]
557+ except PermissionError :
558+ report += [
559+ ("PermissionError " , os .path .join (temp_directory , file )),
560+ ]
561+
562+ active_tab_id = self .n .notebook .select ()
563+ if active_tab_id != "" :
564+ self .n .remove_treeviews (active_tab_id )
565+ self .n .add_treeview (
566+ active_tab_id ,
567+ ("Cleanup" , "file" ),
568+ (report ),
569+ "Cleanup" ,
570+ ".Cleaning tmp files" ,
571+ )
572+
499573 def t_doubleClicked (self , event ):
500574 """launch action when dbl_click on the Database structure"""
501575 # determine item to consider
@@ -592,6 +666,43 @@ def get_tk_icons(self):
592666""" ,
593667 "img_closepressed" : """\
594668 R0lGODdhCAAIAIgAAPAAAP///ywAAAAACAAIAAACDkyAeJYM7FR8Ex7aVpIFADs=
669+ """ ,
670+ "img_run_temp" : """\
671+ R0lGODlhGAAYAHAAACH5BAEAAPwALAAAAAAYABgAhwAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwAr
672+ ZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCq
673+ mQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMA
674+ zDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA
675+ /zOqADOqMzOqZjOqmTOqzDOq/zPVADPVMzPVZjPVmTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YA
676+ AGYAM2YAZmYAmWYAzGYA/2YrAGYrM2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaA
677+ M2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzGaq/2bVAGbVM2bVZmbVmWbVzGbV/2b/AGb/M2b/
678+ Zmb/mWb/zGb//5kAAJkAM5kAZpkAmZkAzJkA/5krAJkrM5krZpkrmZkrzJkr/5lVAJlVM5lVZplV
679+ mZlVzJlV/5mAAJmAM5mAZpmAmZmAzJmA/5mqAJmqM5mqZpmqmZmqzJmq/5nVAJnVM5nVZpnVmZnV
680+ zJnV/5n/AJn/M5n/Zpn/mZn/zJn//8wAAMwAM8wAZswAmcwAzMwA/8wrAMwrM8wrZswrmcwrzMwr
681+ /8xVAMxVM8xVZsxVmcxVzMxV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Bm/zNm/2Zm/5lm/8xm
697+ //+ZAACZADOZAGaZAJmZAMyZAP+ZKwCZKzOZK2aZK5mZK8yZK/+ZVQCZVTOZVWaZVZmZVcyZVf+Z
698+ gACZgDOZgGaZgJmZgMyZgP+ZqgCZqjOZqmaZqpmZqsyZqv+Z1QCZ1TOZ1WaZ1ZmZ1cyZ1f+Z/wCZ
699+ /zOZ/2aZ/5mZ/8yZ///MAADMADPMAGbMAJnMAMzMAP/MKwDMKzPMK2bMK5nMK8zMK//MVQDMVTPM
700+ VWbMVZnMVczMVf/MgADMgDPMgGbMgJnMgMzMgP/MqgDMqjPMqmbMqpnMqszMqv/M1QDM1TPM1WbM
701+ 1ZnM1czM1f/M/wDM/zPM/2bM/5nM/8zM////AAD/ADP/AGb/AJn/AMz/AP//KwD/KzP/K2b/K5n/
702+ K8z/K///VQD/VTP/VWb/VZn/Vcz/Vf//gAD/gDP/gGb/gJn/gMz/gP//qgD/qjP/qmb/qpn/qsz/
703+ qv//1QD/1TP/1Wb/1Zn/1cz/1f///wD//zP//2b//5n//8z///8AAAAAAAAAAAAAAAAIcwD3CRxI
704+ sKDBgwgTKlzIUKEfPw0T/qES8SAViBUL+vmTsSAVjh0H/sEYct/FkgJHojRJMqM9P1VaVrRHE2ZJ
705+ e/BqhsRJM2fHnD1puuQJ9GdQewIBKN23tOnSfTR5FgSQlKlVqlQXZs169anCrQOxrhyLMCAAOw==
595706""" ,
596707 }
597708 return {k : PhotoImage (k , data = v ) for k , v in icons .items ()}
@@ -1079,25 +1190,29 @@ def bip(c):
10791190 sql_error = True
10801191 break
10811192 elif len ("" .join (instruction .split ())) > 1 : # PyPy answer 42 to blanks sql
1193+ nb_columns = - 1
10821194 try :
10831195 if self .output_mode or self .once_mode :
1084- self .conn .export_writer (
1196+ nb_columns = self .conn .export_writer (
10851197 instruction ,
10861198 self .output_file ,
10871199 header = self .default_header ,
10881200 delimiter = self .default_separator ,
10891201 encoding = self .encode_in ,
10901202 initialize = self .init_output ,
10911203 )
1092- self .once_mode , self .init_output = False , False
1093- self .n .add_treeview (
1094- tab_tk_id ,
1095- ("qry" , "file" ),
1096- ((instruction , self .output_file ),),
1097- "Info" ,
1098- ".once %s" % self .output_file ,
1099- )
1100- if self .x_mode :
1204+ if nb_columns > 0 :
1205+ self .once_mode , self .init_output = False , False
1206+ if nb_columns > 0 :
1207+ self .n .add_treeview (
1208+ tab_tk_id ,
1209+ ("qry_to_csv" , "file" ),
1210+ ((instruction , self .output_file ),),
1211+ "Qry" ,
1212+ # ".once %s" % self.output_file,
1213+ first_line ,
1214+ )
1215+ if self .x_mode and nb_columns > 0 :
11011216 os .system (
11021217 "start excel.exe " + self .output_file .replace ("\\ " , "/" )
11031218 )
@@ -2007,9 +2122,14 @@ def export_writer(
20072122 quotechar = '"' ,
20082123 initialize = True ,
20092124 ):
2010- """export a csv table (action) """
2125+ """export a csv table and return number of columns """
20112126 cursor = self .conn .cursor ()
20122127 cursor .execute (sql )
2128+ # do nothing if nothing
2129+ if cursor .description is None or len (cursor .description ) == 0 :
2130+ return - 1
2131+ else :
2132+ nb_columns = len (cursor .description )
20132133 # with PyPy, the "with io.open" for is more than necessary
20142134 if sys .version_info [0 ] != 2 : # python3
20152135 write_mode = "w" if initialize else "a" # Write or Append
@@ -2038,6 +2158,8 @@ def export_writer(
20382158 ) # heading row with anti-PyPy bug
20392159 writer .writerows (cursor .fetchall ())
20402160 fout .close # PyPy3-7.3.5 needs that close
2161+ return nb_columns
2162+
20412163
20422164def _main ():
20432165 welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons)
@@ -2055,25 +2177,17 @@ def _main():
20552177INSERT INTO item values("T","Ford",1000);
20562178INSERT INTO item select "A","Merced",1250 union all select "W","Wheel",9 ;
20572179INSERT INTO part select ItemNo,"W","needed",Kg/250 from item where Kg>250;
2058- \n -- to CREATE a Python embedded function ( enclose them by "py" and ";") :
2180+ \n -- to CREATE a Python embedded function, enclose them by "py" and ";" :
20592181pydef py_hello():
20602182 "hello world"
20612183 return ("Hello, World !");
2062- pydef py_sin(s):
2063- "sinus function : example loading module, handling input/output as strings"
2064- import math as py_math
2065- return ("%s" % py_math.sin(s*1));
20662184pydef py_fib(n):
20672185 "fibonacci : example with function call (may only be internal) "
20682186 fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
20692187 return("%s" % fib(n*1));
2070- pydef py_power(x,
2071- y):
2072- "power function : example loading module, handling input/output as strings"
2073- import math as py_math
2074- return ("%s" % ((x*1) ** (y*1)) );
2075- \n -- to USE a python embedded function and nesting of embedded functions:
2076- select py_hello(), py_sin(1) as sinus, py_power(2, 1*py_fib(6)) as power, sqlite_version();
2188+
2189+ -- to USE a python embedded function and nesting of embedded functions:
2190+ select py_hello(), py_fib(6) as fibonacci, sqlite_version();
20772191\n -- to EXPORT :
20782192-- a TABLE, select TABLE, then click on icon 'SQL->CSV'
20792193-- a QUERY RESULT, select the SCRIPT text, then click on icon '???->CSV',
0 commit comments