@@ -28,9 +28,10 @@ class SqliteDb:
28
28
etc(a, b)
29
29
30
30
"""
31
- def __init__ (self , filename : str , debug : TDebugCtl ) -> None :
31
+ def __init__ (self , filename : str , debug : TDebugCtl , no_disk : bool = False ) -> None :
32
32
self .debug = debug
33
33
self .filename = filename
34
+ self .no_disk = no_disk
34
35
self .nest = 0
35
36
self .con : sqlite3 .Connection | None = None
36
37
@@ -49,7 +50,11 @@ def _connect(self) -> None:
49
50
if self .debug .should ("sql" ):
50
51
self .debug .write (f"Connecting to { self .filename !r} " )
51
52
try :
52
- self .con = sqlite3 .connect (self .filename , check_same_thread = False )
53
+ # Use uri=True when connecting to memory URIs
54
+ if self .filename .startswith ("file:" ):
55
+ self .con = sqlite3 .connect (self .filename , check_same_thread = False , uri = True )
56
+ else :
57
+ self .con = sqlite3 .connect (self .filename , check_same_thread = False )
53
58
except sqlite3 .Error as exc :
54
59
raise DataError (f"Couldn't use data file { self .filename !r} : { exc } " ) from exc
55
60
@@ -78,7 +83,7 @@ def _connect(self) -> None:
78
83
def close (self , force : bool = False ) -> None :
79
84
"""If needed, close the connection."""
80
85
if self .con is not None :
81
- if force or self .filename != ":memory:" :
86
+ if force or not self .no_disk :
82
87
if self .debug .should ("sql" ):
83
88
self .debug .write (f"Closing { self .con !r} on { self .filename !r} " )
84
89
self .con .close ()
@@ -120,7 +125,7 @@ def _execute(self, sql: str, parameters: Iterable[Any]) -> sqlite3.Cursor:
120
125
return self .con .execute (sql , parameters ) # type: ignore[arg-type]
121
126
except sqlite3 .Error as exc :
122
127
msg = str (exc )
123
- if self .filename != ":memory:" :
128
+ if not self .no_disk :
124
129
try :
125
130
# `execute` is the first thing we do with the database, so try
126
131
# hard to provide useful hints if something goes wrong now.
0 commit comments