如何将sqlite表从磁盘数据库复制到python中的内存数据库? [英] How to copy a sqlite table from a disk database to a memory database in python?
本文介绍了如何将sqlite表从磁盘数据库复制到python中的内存数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将基于磁盘的sqlite表复制到python中的内存数据库?我知道表的模式。
How to copy a disk based sqlite table to a memory database in python? I know the schema of the table.
推荐答案
此代码更通用,但也许它可以帮助你:
this code is more general but maybe it can help you:
import sqlite3
new_db = sqlite3.connect(':memory:') # create a memory database
old_db = sqlite3.connect('test.db')
query = "".join(line for line in old_db.iterdump())
# Dump old database in the new one.
new_db.executescript(query)
编辑你的指定表,你可以在for循环中这样改变:
EDIT : for getting your specify table you can just change in the for loop like this:
name_table = "test_table" # name of the table that you want to get.
for line in old_db.iterdump():
if name_table in line:
query = line
break
这篇关于如何将sqlite表从磁盘数据库复制到python中的内存数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文