kuroの覚え書き

96の個人的覚え書き

ディスク保存のデータベースからテーブルをメモリーデータベースに読み込んで利用するには

いろいろやってみているんだけど、なかなか成功しない。
https://stackoverflow.com/questions/4019081/how-to-copy-a-sqlite-table-from-a-disk-database-to-a-memory-database-in-python
https://stackoverflow.com/questions/3850022/how-to-load-existing-db-file-to-memory-in-python-sqlite3
このあたりが方法のようなんだけど、Flaskのwebアプリからうまく動いてくれない。

2つ目のページの内容を使うと

import sqlite3

new_db = sqlite3.connect(':memory:') # create a memory database

old_db = sqlite3.connect('test.db')

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
       new_db.executescript(query)

こんな感じになりそうなんだけど、これをSQLAlchemyと組み合わせると

import sqlite3
from sqlalchemy import create_engine

engine = create_engine('sqlite://')
c = engine.connect()
conmem = c.connection
con = sqlite3.connect('test.db', isolation_level=None)

name_table = "test_table"  # name of the table that you want to get.

for line in con.iterdump():
    if name_table in line:
       query = line
       break
       conmem.executescript(query)

これで行けそうな気がするが

INFO sqlalchemy.engine.base.Engine ()

というメッセージで止まって先に進まない。