ProgrammingError:在线程中创建的SQLite对象只能在同一线程中使用 [英] ProgrammingError: SQLite objects created in a thread can only be used in that same thread

查看:543
本文介绍了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:\Users\app.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对象只能在同一线程中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆