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

后端 未结 6 426
挽巷
挽巷 2020-12-01 04:58

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 o

6条回答
  •  孤城傲影
    2020-12-01 05:33

    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"
    

提交回复
热议问题