我看到了其他有关此的文章,并了解了问题所在。但是,我正在努力编写代码,从而避免了该错误:
from database import db_session
from database import init_db
database.py
engine = create_engine('sqlite:///'+full_path, convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import models
Base.metadata.create_all(bind=engine)
flaskserver.py
@app.route('/process_email')
def process_email():
init_db()
email_address = request.args.get('email_address')
print(email_address)
email_date = datetime.now().strftime('%Y-%m-%d')
rec = db_session.query(EmailCampaign).filter(EmailCampaign.email == email_address).first()
if rec.email_4 is not None:
rec.email_4_respond = email_date
elif rec.email_3 is not None:
rec.email_3_respond = email_date
elif rec.email_2 is not None:
rec.email_2_respond = email_date
elif rec.email_1 is not None:
rec.email_1_resonded = email_date
db_session.commit()
return('done')
我随机得到
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 9396 and this is thread id 13360.
作为错误。我想我必须在process_email()
函数内部初始化连接,对吗?我已经在函数外部尝试过它,但是也遇到了同样的错误。
谢谢。