ProgrammingError:(sqlite3.ProgrammingError)在线程中创建的SQLite对象只能在同一线程中使用

时间:2019-07-08 01:34:03

标签: python sqlalchemy

我需要有关此代码的帮助。

每当我运行代码时,我都会遇到以下问题: ProgrammingError:(sqlite3.ProgrammingError)在线程中创建的SQLite对象只能在同一线程中使用。该对象在线程ID 140661091202816中创建,这是线程ID 140661082810112 [SQL:选择country.name AS country_name,country.id AS country_id 来自国家

from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Country, CountryItem
app = Flask(__name__)


engine = create_engine('sqlite:///countryfood.db')
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()


@app.route('/')
@app.route('/hello')
def HelloWorld():
    country = session.query(Country).first()
    items = session.query(CountryItem).filter_by(country_id = country.id)
    output = ''
    for i in items:
        output += str(i.id)
        output += ':       '
        output += i.name
        output += '</br>'

    return output

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)

国家/地区= session.query(国家/地区).first()     items = session.query(CountryItem).filter_by(country_id = country.id)

请帮助!

提前谢谢

这是我的代码:

country = session.query(Country).first()
items = session.query(CountryItem).filter_by(country_id = country.id)

0 个答案:

没有答案