我有一段代码:
from io import BytesIO as Memory
import requests
def download_file_to_obj(url, file_obj):
with requests.get(url, stream=True) as r:
r.raise_for_status()
for chunk in r.iter_content(chunk_size=None):
if chunk:
file_obj.write(chunk)
def main(source_url):
db_in_mem = Memory()
print('Downloading..')
download_file_to_obj(source_url, db_in_mem)
print('Complete!')
with sqlite3.connect(database=db_in_mem.read()) as con:
cursor = con.cursor()
cursor.execute('SELECT * FROM my_table limit 10;')
data = cursor.fetchall()
print(data)
del(db_in_mem)
my_table 在源数据库中存在。 错误信息:
sqlite3.OperationalError: 没有这样的表:my_table
如何从http加载SQLite数据库到内存中?
:memory:
内存数据库。即使备份API也只知道如何在它们之间进行复制,但据我所知,没有一种方法能够使用Python文件对象或内存映像。如果您想走这条路,您将不得不找到一个内存文件系统。您可以查看pyfakefs。由您决定是否值得,或者是否使用真正的文件更简单... - Serge Ballesta