我试图显示在test.db文件中所做的所有输入,但是IDE抛出以下错误:
Usr.query.all()
回溯(最近通话最近一次):
**(sqlite3.ProgrammingError) **SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 16064 and this is thread id 9372.**
[SQL: SELECT usr.id AS usr_id, usr.username AS usr_username, usr.email AS usr_email, usr.profile_img AS usr_profile_img, usr.password AS usr_password
FROM usr]
[parameters: [{}]]**
(此错误的背景位于:http://sqlalche.me/e/f405)
有人知道这意味着什么吗?
以下是我的app.py来源
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Usr(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.VARCHAR, unique=True, nullable=False)
email = db.Column(db.VARCHAR(64), unique=True, nullable=False)
profile_img = db.Column(db.VARCHAR(32), nullable=False, default='default.jpg')
password = db.Column(db.VARCHAR(40), nullable=False)
tweets = db.relationship('Tweet', backref='author', lazy=True)
def __repr__(self):
return f"Usr('{self.username}', '{self.email}', '{self.profile_img}')"
class Tweet(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.Text(64), )
content = db.Column(db.NVARCHAR(280), nullable = False)
date_modified = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('usr.id'), nullable=False)
def __repr__(self):
return f"Tweet('{self.title}', '{self.date_modified}')"@app.route('/')
@app.route('/home')
def home():
return render_template('home.html', blog_db=blogs, headlines=news)
@app.route('/register', methods=['GET', 'POST'])
def registration():
form = Register_Form()
if form.validate_on_submit():
flash(f'Welcome {form.username.data}, your account has been created!', 'success')
return redirect(url_for('home'))
return render_template('signup.html', title='Sign Up', form=form)
@app.route('/login', methods=['GET', 'POST'])
def usr_login():
form = Login_Form()
if form.validate_on_submit():
if form.username.data == 'analfister' and form.password.data == 'sex-t9':
flash('Successfully logged in', 'success')
return redirect(url_for('home'))
else:
flash('You aight man? cause I found zero records of your credentials', 'danger')
return render_template('login.html', title='Login', form=form)
@app.route('/about')
def bleep():
return render_template('about.html', title='About')
if __name__ == '__main__':
app.run(debug=True)
这些是我直接将db查询及其各自的输出插入到python控制台中的
from app import db
F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\flask_sqlalchemy\__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
db.create_all()
from app import Usr, Tweet
user0=Usr(username='test0', email='test0@pychat.app', password='TesT@0')
db.session.add(user0)
user1=Usr(username='test1', email='test1@pychat.app', password='TesT@1')
db.session.add(user1)
db.session.commit()
Usr.query.all()
Traceback (most recent call last):
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1179, in _execute_context
context = constructor(dialect, self, conn, *args)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\default.py", line 688, in _init_compiled
self.cursor = self.create_cursor()
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1042, in create_cursor
return self._dbapi_connection.cursor()
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\pool\base.py", line 965, in cursor
return self.connection.cursor(*args, **kwargs)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 16064 and this is thread id 9372.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3178, in all
return list(self)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3334, in __iter__
return self._execute_and_instances(context)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3359, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\base.py", line 988, in execute
return meth(self, multiparams, params)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1107, in _execute_clauseelement
distilled_params,
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
e, util.text_type(statement), parameters, None, None
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1473, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1179, in _execute_context
context = constructor(dialect, self, conn, *args)
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\default.py", line 688, in _init_compiled
self.cursor = self.create_cursor()
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1042, in create_cursor
return self._dbapi_connection.cursor()
File "F:\PycharmProjects\BulletinBoard\venv\lib\site-packages\sqlalchemy\pool\base.py", line 965, in cursor
return self.connection.cursor(*args, **kwargs)
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 16064 and this is thread id 9372.
[SQL: SELECT usr.id AS usr_id, usr.username AS usr_username, usr.email AS usr_email, usr.profile_img AS usr_profile_img, usr.password AS usr_password
FROM usr]
[parameters: [{}]]
(Background on this error at: http://sqlalche.me/e/f405)