SQLite只读模式查询后残留db-shm与db-wal文件问题求助
SQLite只读模式查询后残留db-shm与db-wal文件问题求助
我现在需要写一段基础代码,以只读模式对SQLite数据库执行查询操作。这些数据库都是每日生成的文件,所以特别关键的一点是:关闭连接后不能在服务器上留下db-shm或db-wal这类关联文件。
我查了官方文档,尝试显式关闭连接,但这些连接似乎并没有真正被关闭,导致db-shm和db-wal文件一直残留在服务器上。以下是我目前的实现代码:
import sqlite3 import pandas as pd def _connect(f): con = None try: con = sqlite3.connect("file:"+f+"?mode=ro", uri=True) except sqlite3.Error as er: print('SQLite error in the db connection: %s' % (' '.join(er.args))) return con def _disconnect(con): try: con.close() except sqlite3.Error as er: print('SQLite error in the db disconnection: %s' % (' '.join(er.args))) return con def fl_query(file): '''Returns information about a db file file : string absolute path to the db file returns ------- list ''' cn = _connect(file) cur = cn.cursor() query = """SELECT ....etc""" cur.execute(query) info = [(row[0],row[1],row[2],row[3],row[4],row[5]) for row in cur.fetchall()] cur.close() _disconnect(cn) return info folder = 'path to the db file' file = folder+'6100000_2022-09-18.db' info = fl_query(file)
我已经尝试过各种关于干净关闭数据库连接的方法,但至今都没有效果,每次打开文件后db-shm和db-wal文件都会遗留下来。这里要特别说明:服务器上有上千个这类每日生成的数据库文件,所以绝对不能产生额外的关联文件,否则会造成严重的存储和管理问题。
备注:内容来源于stack exchange,提问作者gis20




