Skip to content

Commit 653d700

Browse files
committed
add basic read only mode
1 parent 7da102b commit 653d700

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

qcodes/dataset/sqlite/database.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ def _adapt_complex(value: complex | np.complexfloating) -> sqlite3.Binary:
115115
return sqlite3.Binary(out.read())
116116

117117

118-
def connect(name: str | Path, debug: bool = False, version: int = -1) -> ConnectionPlus:
118+
def connect(
119+
name: str | Path, debug: bool = False, version: int = -1, read_only: bool = False
120+
) -> ConnectionPlus:
119121
"""
120122
Connect or create database. If debug the queries will be echoed back.
121123
This function takes care of registering the numpy/sqlite type
@@ -126,6 +128,7 @@ def connect(name: str | Path, debug: bool = False, version: int = -1) -> Connect
126128
debug: should tracing be turned on.
127129
version: which version to create. We count from 0. -1 means 'latest'.
128130
Should always be left at -1 except when testing.
131+
read_only: Should the database be opened in read only mode.
129132
130133
Returns:
131134
connection object to the database (note, it is
@@ -137,8 +140,14 @@ def connect(name: str | Path, debug: bool = False, version: int = -1) -> Connect
137140
# register binary(TEXT) -> numpy converter
138141
sqlite3.register_converter("array", _convert_array)
139142

140-
sqlite3_conn = sqlite3.connect(name, detect_types=sqlite3.PARSE_DECLTYPES,
141-
check_same_thread=True)
143+
path = f"file:{str(name)}"
144+
145+
if read_only:
146+
path = path + "?ro"
147+
148+
sqlite3_conn = sqlite3.connect(
149+
path, detect_types=sqlite3.PARSE_DECLTYPES, check_same_thread=True, uri=True
150+
)
142151
conn = ConnectionPlus(sqlite3_conn)
143152

144153
latest_supported_version = _latest_available_version()

0 commit comments

Comments
 (0)