ProgrammingError: SQLite objects created in a thread can only be used in that same thread

Multi tool use
Multi tool use


ProgrammingError: SQLite objects created in a thread can only be used in that same thread



i'm fairly new to programming. I've tried MySQL before, but now it's my first time using SQLite in a python flask website.
So maybe I'm using MySQL syntax instead of SQLite, but I can't seem to find the problem.


Piece of my code:

@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method=='POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))

c.execute("INSERT INTO users(name,email,username,password)
VALUES(?,?,?,?)", (name, email, username, password))

conn.commit

conn.close()

The error:
File "C:Usersapp.py", line 59, in register c.execute("INSERT INTO users(name,email,username,password) VALUES(?,?,?,?)", (name, email, username, password))
ProgrammingError: SQLite objects created in a thread can only be used in that
same thread.The object was created in thread id 23508 and this is thread id
22640



Does this mean I can't use the name, email username & password in an HTML file?
How do I solve this?



Thank you.





Are name, email, username & password SQLite Objects? Read the words in the error message. They're there for reasons other than taking up space on your screen.
– Ken White
Jan 12 at 0:58





No, they're clearly python objects defined right above it. The error message talks about the connection and the cursor.
– ndrix
Jan 12 at 1:18




3 Answers
3



Your cursor 'c' is not created in the same thread; it was probably initialized when the Flask app is ran.



You probably want to generate SQLite objects (the conneciton, and the cursor) in the same method, such as:


@app.route('/')
def dostuff():
with sql.connect("database.db") as con:
name = "bob"
cur = con.cursor()
cur.execute("INSERT INTO students (name) VALUES (?)",(bob))
con.commit()
msg = "Done"



Where you make your connection to the database add the following.


conn = sqlite3.connect('your.db', check_same_thread=False)





is this safe to use?
– uzu
Apr 4 at 10:35





@uzu, I don't see why not, as long as you do your own synchronization to ensure only one thread uses the object at the same time.
– merlin2011
Jun 21 at 3:39






Some additional info for future readers of this thread. Per docs.python.org/3/library/sqlite3.html: By default, check_same_thread is True and only the creating thread may use the connection. If set False, the returned connection may be shared across multiple threads. When using multiple threads with the same connection writing operations should be serialized by the user to avoid data corruption.
– Snidhi Sofpro
Jun 26 at 10:33




In my case, I have the same issue with two python files creating sqlite engine and therefore possibly operating on different threads. Reading SQLAlchemy doc here, it seems it is better to use singleton technique in both files:


# maintain the same connection per thread
from sqlalchemy.pool import SingletonThreadPool
engine = create_engine('sqlite:///mydb.db',
poolclass=SingletonThreadPool)



It does not solve all cases, meaning I occasionally getting the same error, but i can easily overcome it, refreshing the browser page. Since I'm only using this to debug my code, this is OK for me. For more permanent solution, should probably choose another database, like PostgreSQL or other database






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Hq3,X,UxUvIyQP DQyi PJEVIaZ81A,M YQ,46RaSVYt,Phd95,0Td1nSirQstI,ksyG
bTB6Y9t3q,6GflC58vtJjEe RVgV,JuCgQ,3HgG6 CcEs ALrSAW,5Nh ZrahXui8I19 esh0nF0 5uID

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications