如何使用python设置sqlite TEMP_STORE为3



我正在使用python和sqlite3,并希望将内存用于临时文件。根据文档,https://www.sqlite.org/compile.html, SQLITE_TEMP_STORE=3表示"始终使用内存"。我可以检查当前值:

import sqlite3
conn = sqlite3.connect("test.db")
cur = conn.cursor()
check_db = conn.execute( """ select * from pragma_compile_options where compile_options like 'TEMP_STORE=%' """).fetchall() 
print("check_db:", check_db)

当我尝试更新:

sq_update = """ update pragma_compile_options set compile_options = 'TEMP_STORE=3' where compile_options like 'TEMP_STORE=1' """ 
conn.execute(sq_update) conn.commit() 

返回以下错误。INTERNALERROR>sqlite3。OperationalError: table pragma_compile_options可能无法修改

我的目标是设置告诉sqlite使用临时文件的内存

您需要检查pragma_compile_options输出的内容,以查看TEMP_STORE的值。如果TEMP_STORE显式设置为非零值,则只能更改运行时设置。在这种情况下,使用PRAGMA temp_store = 2来实现您的目标。看到https://www.sqlite.org/pragma.html pragma_temp_store .

最新更新