【问题标题】:I wolud like to copy sqlite3 db from memory to hard drive. How can I do? [duplicate]我想将 sqlite3 db 从内存复制到硬盘。我能怎么做? [复制]
【发布时间】:2015-01-27 10:17:35
【问题描述】:

我想将 sqlite db 从内存复制到硬盘。我该怎么办?

我试试这个方法:

conn_phy = sqlite3.connect("phy.db")
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c_phy = conn.cursor()

for row in c.execute("select * from table"):
    c_phy.execute(' insert into tablephy (column1, column2, column3) values\
     (?,?,?)', row) #phy db column number equal memory column number
    conn_phy.commit()

【问题讨论】:

  • 你可以转储它。更多信息:how do i dump a single sqlite3 table in python?
  • 好像有错别字,你的意思可能是:c_phy = conn_phy.cursor()。您是否尝试将 conn_phy 而不是 conn 传递给填充 conn 的代码?

标签: python-3.x sqlite


【解决方案1】:

sqlite3 Python 模块不公开sqlite3_backup_*() C functions。可能没有直接方法可以将内存数据库保存到磁盘上的数据库文件中。

可以使用old_db.iterdump() + new_db.executescript() 的模拟(未测试):

import sqlite3

old_db = sqlite3.connect(':memory:')
# here's code that works with old_db
# ...

# dump old database in the new one
with sqlite3.connect('test.db') as new_db:
    new_db.executescript("".join(old_db.iterdump()))

基于@mouad's answer to the opposite quesiton (loading from file to in-memory db)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 2020-09-16
    • 2015-11-13
    • 2022-07-20
    • 2015-05-01
    • 1970-01-01
    • 2011-10-22
    • 2018-10-20
    相关资源
    最近更新 更多