I have a server which allows (admins) to create,remove or edit other user's details, I have created the database alongside its cursors inside a function:
def DBSetup():
global schoolDBConn, schoolDBCursor
print("Setting up databases")
schoolDBConn = sqlite3.connect("SCHOOL_DB.db")
schoolDBCursor = schoolDBConn.cursor()
schoolDBCursor.execute(
"""
CREATE TABLE IF NOT EXISTS USER_DETAILS
(
username text,
password text,
clearance int,
classes int
)
"""
)
schoolDBCursor.execute( #line 300
"""
CREATE TABLE IF NOT EXISTS CLASSES
(
className text,
supervisor text,
assignmentName text
)
"""
)
schoolDBCursor.execute(
"""
CREATE TABLE IF NOT EXISTS ASSIGNMENT
(
setDate text,
dueDate text,
assignmentInfo text,
supervisor text
)
"""
)
I call this when the server starts up, and then I begin my thread:
if __name__ == "__main__":
DBSetup()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()
However inside my thread, whenever I use a function that edits the database, i get this error:
SQLITE3 ERROR:
SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 9628 and this is thread id 12400
I am using global locks in my program "With global_lock:" whenever I edit a database.
Thank you in advance