Created
November 26, 2018 07:48
-
-
Save disktnk/cdaf61962134fc03512e9cabbc8f30f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's better to use
with closing
statement