Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I can't find the problem:

@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()

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

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

like image 307
Tania Avatar asked Jan 12 '18 00:01

Tania


4 Answers

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

conn = sqlite3.connect('your.db', check_same_thread=False)
like image 137
cmrussell Avatar answered Nov 18 '22 20:11

cmrussell


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

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"
like image 43
ndrix Avatar answered Nov 18 '22 18:11

ndrix


engine = create_engine(
'sqlite:///restaurantmenu.db',
connect_args={'check_same_thread': False}
)

Works for me

like image 49
J J Avatar answered Nov 18 '22 18:11

J J


You can try this:

engine=create_engine('sqlite:///data.db', echo=True, connect_args={"check_same_thread": False})

It worked for me

like image 7
Asgar Avatar answered Nov 18 '22 18:11

Asgar