我正在尝试编写一个脚本来导入数据库文件。我像这样编写了导出文件的脚本:
import sqlite3
con = sqlite3.connect('../sqlite.db')
with open('../dump.sql', 'w') as f:
for line in con.iterdump():
f.write('%s\n' % line)现在,我希望能够导入该数据库。我试过了:
import sqlite3
con = sqlite3.connect('../sqlite.db')
f = open('../dump.sql','r')
str = f.read()
con.execute(str)但我不能执行多条语句。有没有办法让它直接运行SQL脚本?
发布于 2011-01-18 07:50:19
sql = f.read() # watch out for built-in `str`
cur.executescript(sql)Documentation。
发布于 2011-01-18 07:50:39
尝试使用
con.executescript(str)文档
Connection.executescript(sql_script)
This is a nonstandard shortcut that creates an intermediate cursor object
by calling the cursor method, then calls the cursor’s executescript
method with the parameters given.或者先创建光标
import sqlite3
con = sqlite3.connect('../sqlite.db')
f = open('../dump.sql','r')
str = f.read()
cur = con.cursor()
cur.execute(str)https://stackoverflow.com/questions/4719159
复制相似问题