我在尝试创建一个数据库以下 flask 教程卡住:https://www.youtube.com/watch?v=cYWiDiIUxQc&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH&index=4
我没有成功地搜索到这个问题的解决方案。请帮助。
当我尝试在从项目导入db后运行db.create_all()
时,我得到
sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)无法打开数据库文件
简体中文
from datetime import datetime
from flask import Flask, render_template, url_for, flash, redirect
from flask_sqlalchemy import SQLAlchemy
from forms import RegistrationForm, LoginForm
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
错误:
db.create_all()Traceback(最后一次调用):文件“/home/kbyrd/anaconda 3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/base.py“,第2339行,in _wrap_pool_connect返回fn()File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 364,在连接中return _ConnectionFairy._checkout(self)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 778,in _checkout fairy = _ConnectionRecord.checkout(pool)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 495,in checkout rec = pool._do_get()File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/impl.py“,第239行,in _do_get return self._create_connection()File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py“,第309行,in _create_connection return _ConnectionRecord(self)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 440,在initself.__connect(first_connect_check=True)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 661,在connect pool.logger.debug(“Error on connect():%s”,e)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py“,line 68,inexit**compat.raise*(File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/util/compat.py“,line 178,在raise* raise exception文件“/home/kbyrd/anaconda 3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”第656行中,in __connect connection = pool._invoke_creator(self)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py“,line 114,in connect return dialect.connect(*cargs,**cparams)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/default.py“,line 493,in connect return self.dbapi.connect(*cargs,**cparams)sqlite3.OperationalError:无法打开数据库文件
上述异常是以下异常的直接原因:
追溯(最近一次调用): 文件“",line 1,in文件“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/flask_sqlalchemy/init.py”,line 1039,in create_all self._execute_for_all_tables(app,bind,'create_all')文件“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/flask_sqlalchemy/init.py”,line 1031,in _execute_for_all_tables op(bind=self.get_engine(app,bind),extra)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/sql/schema.py“,line 4546,in create_all bind._run_visitor(File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/ www.example.com ”,line 2097,in _run_visitor with self._optional_conn_ctx_manager(connection)as conn:File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/contextlib.py“,line 113,inenterreturn next(self.gen)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/base.py“,line 2089,in _optional_conn_ctx_manager with self._contextual_connect()as conn:文件“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/base.py”,line 2305,in _contextual_connect self._wrap_pool_connect(self.pool.connect,None),文件“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/base.py“,line 2342,in _wrap_pool_connect Connection.handle_dbapi_exception_noconnection(File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/base.py”,line 1584,在 handle_dbapi_exception_noconnection util.raise(文件“/home/kbyrd/anaconda 3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/util/compat.py“,第178行,在raise raise exception文件“/home/kbyrd/anaconda 3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/base.py”中,第2339行,in _wrap_pool_connect返回fn()File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 364,在连接中return _ConnectionFairy._checkout(self)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 778,in _checkout fairy = _ConnectionRecord.checkout(pool)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 495,in checkout rec = pool._do_get()File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/impl.py“,line 239,in _do_get return self._create_connection()File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py“,line 309,in _create_connection return _ConnectionRecord(self)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 440,在initself.__connect(first_connect_check=True)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”,line 661,在connect pool.logger.debug(“Error on connect():%s”,e)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py“,line 68,inexitcompat.raise*(File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/util/compat.py“,line 178,在raise* raise exception文件“/home/kbyrd/anaconda 3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/pool/base.py”第656行中,in __connect connection = pool._invoke_creator(self)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py“,line 114,in connect return dialect.connect(*cargs,**cparams)File“/home/kbyrd/anaconda3/envs/flasksite/lib/python3.8/site-packages/sqlalchemy/engine/default.py“,line 493,in connect return self.dbapi.connect(*cargs,**cparams)sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)无法打开数据库文件(此错误的背景位于:http://sqlalche.me/e/13/e3q8)
3条答案
按热度按时间wfsdck301#
参考Miguel https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database的出色工作。也许你需要让应用程序知道basedir路径?注意他使用app.db而不是site.db Eg。
然后他初始化db
希望这能帮上一点忙。
hiz5n14c2#
af7jpaap3#
我发现了问题所在。我的程序使用SMB保存在共享中。当我保存到tmp目录时,我能够创建数据库。我还没有弄清楚如何在共享上创建数据库,但我的目标无论如何都是通过创建数据库并向其提交记录来实现的。