@@ -137,6 +137,10 @@ 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 ):
141+ 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 )
140144 self .menu .add_separator ()
141145 self .menu .add_command (label = "Quit" , command = self .quit_db )
142146
@@ -234,6 +238,43 @@ def open_db(self, filename="", isolation_level=None):
234238 self .conn = Baresql (self .database_file )
235239 self .actualize_db ()
236240
241+ def backup_db (self , filename = "" , isolation_level = None ):
242+ """Backup the current database"""
243+ if filename == "" :
244+ filename = filedialog .asksaveasfilename (
245+ initialdir = self .initialdir ,
246+ defaultextension = ".db" ,
247+ title = "Define a new database name and location" ,
248+ filetypes = [("default" , "*.db" ), ("other" , "*.db*" ), ("all" , "*.*" )],
249+ )
250+ if filename != "" :
251+ if os .path .isfile (filename ):
252+ self .set_initialdir (filename )
253+ if messagebox .askyesno (
254+ message = "Confirm Destruction of previous Datas ?" ,
255+ icon = "question" ,
256+ title = "Destroying" ,
257+ ):
258+ os .remove (filename )
259+ db_to = sqlite .connect (filename )
260+ self .conn .conn .backup (db_to )
261+ db_to .close ()
262+ self .actualize_db ()
263+
264+ def restore_db (self , filename = "" , isolation_level = None ):
265+ """Restore an existing database into current one"""
266+ if filename == "" :
267+ filename = filedialog .askopenfilename (
268+ initialdir = self .initialdir ,
269+ defaultextension = ".db" ,
270+ filetypes = [("default" , "*.db" ), ("other" , "*.db*" ), ("all" , "*.*" )],
271+ )
272+ if filename != "" :
273+ db_from = sqlite .connect (filename )
274+ db_from .backup (self .conn .conn )
275+ db_from .close
276+ self .actualize_db ()
277+
237278 def load_script (self ):
238279 """load a script file, ask validation of detected Python code"""
239280 filename = filedialog .askopenfilename (
@@ -1013,17 +1054,15 @@ def bip(c):
10131054 if (filename + "z" )[0 ] == "~" :
10141055 filename = os .path .join (self .home , filename [1 :])
10151056 db_from = sqlite .connect (filename )
1016- with db_from :
1017- db_from .backup (self .conn .conn )
1057+ db_from .backup (self .conn .conn )
10181058 db_from .close
10191059 self .actualize_db ()
10201060 if shell_list [0 ] == ".backup" and len (shell_list ) >= 2 :
10211061 filename = shell_list [1 ]
10221062 if (filename + "z" )[0 ] == "~" :
10231063 filename = os .path .join (self .home , filename [1 :])
10241064 db_to = sqlite .connect (filename )
1025- with db_to :
1026- self .conn .conn .backup (db_to )
1065+ self .conn .conn .backup (db_to )
10271066 db_to .close ()
10281067 if shell_list [0 ] == ".shell" and len (shell_list ) >= 2 :
10291068 os .system (instru [len (".print" ) + 1 :] + "\n " )
@@ -1983,6 +2022,7 @@ def export_writer(
19832022 [i if isinstance (i , str ) else i [0 ] for i in cursor .description ]
19842023 ) # PyPy as a strange list of list
19852024 writer .writerows (cursor .fetchall ())
2025+ fout .close # PyPy3-7.3.5 needs that close
19862026 else : # python2.7 (minimal)
19872027 write_mode = "wb" if initialize else "ab" # Write or Append
19882028 with io .open (csv_file , write_mode ) as fout :
@@ -1997,7 +2037,7 @@ def export_writer(
19972037 [i if isinstance (i , str ) else i [0 ] for i in cursor .description ]
19982038 ) # heading row with anti-PyPy bug
19992039 writer .writerows (cursor .fetchall ())
2000-
2040+ fout . close # PyPy3-7.3.5 needs that close
20012041
20022042def _main ():
20032043 welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons)
@@ -2056,7 +2096,7 @@ def _main():
20562096RELEASE SAVEPOINT remember_Neo; -- free memory
20572097
20582098\n \n -- '.' commands understood:
2059- -- .backup FILE Backup DB (default "main") to FILE
2099+ -- .backup FILE Backup DB (default "main") to FILE (if Python>=3.7)
20602100-- .cd DIRECTORY Change the working directory to DIRECTORY
20612101-- .dump ?FILE? Render database content as SQL (to FILE if specified)
20622102-- .excel Display the output of next command in spreadsheet
@@ -2068,7 +2108,7 @@ def _main():
20682108-- .output ?FILE? Send output to FILE or stdout if FILE is omitted
20692109-- .print STRING... Print literal STRING
20702110-- .read FILE Read input from FILE
2071- -- .restore FILE Restore content of DB (default "main") from FILE
2111+ -- .restore FILE Restore DB (default "main") from FILE (if Python>=3.7)
20722112-- .separator COL Set column separator in next .once exports (default ,)
20732113-- .shell CMD ARGS... Run CMD ARGS... in a system shell
20742114
0 commit comments