I am trying to pass data from a dict into a database table using sqlite3.
The first iteration runs smoothly, afterwards I receive a disk I/O error. Upon rerunning the Code I am confronted with “database is locked” errors until I restart everything or delete the database.
Here is my code for initiating the database and table (directly in main script)
import sqlite3
from utils import nested_loop_function as utils
try:
con = sqlite3.connect(
"<path to DB>/structures.db",
timeout=30,
)
cur = con.cursor()
cur.execute(
"""CREATE TABLE IF NOT EXISTS combinations (
hash_ID TEXT NOT NULL PRIMARY KEY,
path TEXT,
u1_name TEXT,
u2_name TEXT,
central_H_index_1 INTEGER,
central_H_index_2 INTEGER,
charge_u1 INTEGER,
charge_u2 INTEGER,
translation FLOAT,
rotation INTEGER,
tilt_angle FLOAT,
tilt_axis TEXT,
hash_u1 INTEGER,
hash_u2 INTEGER
)"""
)
print("Sucessfully created table if not present")
con.commit()
except Exception as e:
print(f"An error occurred: {e}")
con.rollback()
finally:
con.close()
Here is my code for inserting data, which iterates in a nested ‘for’ loop. Respective function imported from utils.py.
try:
con = sqlite3.connect(
"<path to DB>/structures.db",
timeout=30,
)
cur = con.cursor()
cur.execute(
"""REPLACE INTO combinations (
hash_ID,
path,
u1_name,
u2_name,
central_H_index_1,
central_H_index_2,
charge_u1,
charge_u2,
translation,
rotation,
tilt_angle,
tilt_axis,
hash_u1,
hash_u2
) VALUES (
:hash_ID,
:path,
:u1_name,
:u2_name,
:central_atom_idx_1,
:central_atom_idx_2,
:charge_u1,
:charge_u2,
:translation,
:rotation,
:tilt_angle,
:tilt_axis,
:hash_u1,
:hash_u2
)""",
meta,
)
con.commit()
except Exception as e:
print(f"An error occurred: {e}")
stop = True
con.rollback()
finally:
con.close()
if stop:
print(f"stopped function")
break
I am really at my wits end here so any help is welcome!
The database and every script are saved on our shared directories but only I can access it.
Running lsof on the database file shows no active processess accessing it once the script fails.
Expected the Code to INSERT/REPLACE data from meta into my combinations table in the structures database. Which it only did for one iteration before returning above ‘disk I/O’ errors. Researching my error resulted in no useful information. Rerunning the code resulted in a ‘locked database’ error. Using context managing ‘with sqlite3.connect(”) as con:’, did not help either and presented the same disk I/O errors. The error occurs when running the code as interactive cells in VSC and when running the entire script from the terminal.
MolecularDyonisus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.