Skip to content

Instantly share code, notes, and snippets.

@disktnk
Created November 26, 2018 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disktnk/cdaf61962134fc03512e9cabbc8f30f7 to your computer and use it in GitHub Desktop.
Save disktnk/cdaf61962134fc03512e9cabbc8f30f7 to your computer and use it in GitHub Desktop.
class Database(object):
def dump(self, url):
import sqlite3
db = sqlite3.connect(url)
script = '\n'.join(db.iterdump())
with open('test.sql', 'w') as f:
f.write(script)
db.close()
def load(self):
with open('test.sql') as f:
sql = ''
for line in f:
line = line.strip()
if line.startswith('--'):
continue
sql += line
if not line.endswith(';'):
continue
try:
self.session.execute(sql)
except Exception as e:
print(e)
finally:
sql = ''
@disktnk
Copy link
Author

disktnk commented Nov 26, 2018

it's better to use with closing statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment