Python和sqlite3-导入和导出数据库 [英] Python and sqlite3 - importing and exporting databases

查看:707
本文介绍了Python和sqlite3-导入和导出数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写脚本来导入数据库文件。我写了脚本来像这样导出文件:

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 have tried :

import sqlite3

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

但我不允许执行多个语句。有没有办法让它直接运行SQL脚本?

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

推荐答案

sql = f.read() # watch out for built-in `str`
cur.executescript(sql)

文档

这篇关于Python和sqlite3-导入和导出数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆