我正在关注此页面的教程:
https://pythonhosted.org/Flask-SQLAlchemy/quickstart.html
我相信我完全符合直线。但每当我尝试db.create_all()
时,我都会收到错误这是我的代码(为了简单起见,我将实际的db.create_all()移动到了文件的一部分,尽管我已经按照教程在命令行中显示的方式进行了尝试。
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return '<User %r>' % self.username
db.create_all()
我在Stack Overflow上发现了一个类似的问题 Flask SQLAlchemy db.create_all() not creating database
和
SQLAlchemy create_all() does not create tables
但这种反应对我没有帮助。 (事实上,第二个问题的答案似乎有一个严重的错误)
这完全在Python 3.4中
我得到的错误很长,但现在是:
Traceback (most recent call last):
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\default.py", line 377, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlite3.OperationalError: unable to open database file
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import test_create_db.py
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\test_create_db.py", line 25, in <module>
db.create_all()
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\flask_sqlalchemy\__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\flask_sqlalchemy\__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\sql\schema.py", line 3404, in create_all
tables=tables)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\base.py", line 1615, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "C:\Python34\Lib\contextlib.py", line 59, in __enter__
return next(self.gen)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\base.py", line 1608, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\base.py", line 1798, in contextual_connect
self.pool.connect(),
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 338, in connect
return _ConnectionFairy._checkout(self)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 644, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
rec = pool._do_get()
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 1057, in _do_get
return self._create_connection()
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
return _ConnectionRecord(self)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 411, in __init__
self.connection = self.__connect()
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 538, in __connect
connection = self.__pool._creator()
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\strategies.py", line 96, in connect
connection_invalidated=invalidated
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\util\compat.py", line 188, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\util\compat.py", line 181, in reraise
raise value.with_traceback(tb)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\default.py", line 377, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None
答案 0 :(得分:5)
您使用的是Windows,Windows中没有/tmp
临时目录。数据库连接失败,因为SQLite3没有用于创建数据库文件的目录。为数据库文件选择一个不同的位置。
在这种情况下,您可以使用tempfile.gettempdir()
来构建路径:
import tempfile
import os.path
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
tempfile.gettempdir(), 'test.db')