ProgrammingError: SQLite objects created in a thread can only be used in that same thread(ProgrammingError: 在一个线程中创建的 SQLite 对象只能在同一个线程中使用)
问题描述
我对编程还很陌生.我之前尝试过 MySQL,但现在是我第一次在 python Flask 网站中使用 SQLite.所以也许我使用的是 MySQL 语法而不是 SQLite,但我似乎找不到问题所在.
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
这是否意味着我不能使用姓名、电子邮件用户名和HTML 文件中的密码?我该如何解决这个问题?
Does this mean I can't use the name, email username & password in an HTML file? How do I solve this?
谢谢.
推荐答案
你的光标 'c' 不是在同一个线程中创建的;它可能是在运行 Flask 应用程序时初始化的.
Your cursor 'c' is not created in the same thread; it was probably initialized when the Flask app was run.
您可能希望以相同的方法生成 SQLite 对象(连接器和游标),例如:
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 (?)",(name))
con.commit()
msg = "Done"
这篇关于ProgrammingError: 在一个线程中创建的 SQLite 对象只能在同一个线程中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ProgrammingError: 在一个线程中创建的 SQLite 对象只能在同一个线程中使用
基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
