Python and sqlite3 - importing and exporting databases

Posted by JPC on Stack Overflow See other posts from Stack Overflow or by JPC
Published on 2011-01-17T23:45:44Z Indexed on 2011/01/17 23:53 UTC
Read the original article Hit count: 226

Filed under:
|
|
|
|

I'm trying to write a script to import a database file. I wrote the script to export the file like so: import sqlite3

con = sqlite3.connect('../sqlite.db')
with open('../dump.sql', 'w') as f:
    for line in con.iterdump():
        f.write('%s\n' % line)

Now I want to be able to import that database. I tried:

import sqlite3

con = sqlite3.connect('../sqlite.db')
f = open('../dump.sql','r')
str = f.read()
con.execute(str)

but I'm not allowed to execute more than one statement. Is there a way to get it to run a .sql script directly?

© Stack Overflow or respective owner

Related posts about python

Related posts about sql