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

后端 未结 6 422
挽巷
挽巷 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:43

    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

提交回复
热议问题