简体   繁体   English

创建数据库后出现未知数据库错误(sqlalchemy)

[英]Unknown database error after creating it (sqlalchemy)

I'm getting the next error while trying to run my Flask app: 尝试运行Flask应用时出现下一个错误:

sqlalchemy.exc.OperationalError
OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database '/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db'")

so it seems like there is no database.. but I ran the next scripts using sqlalchemy_utils and everything was ok: 所以似乎没有数据库..但是我使用sqlalchemy_utils运行了下一个脚本,一切正常:

engine = create_engine("mysql://root:@localhost/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db")

create_database(engine.url)

but still I get the error.. 但还是我得到了错误。

You have confused MySQL, a database server, with SQLite, a database in a file. 您已经将数据库服务器MySQL与文件中的数据库SQLite混淆了。 You created a SQLite file, but are trying to tell MySQL to connect to it, which makes no sense. 您创建了一个SQLite文件,但是试图告诉MySQL连接到它,这没有任何意义。

Use the sqlite dialect in the connection string. 在连接字符串中使用sqlite方言。

You can avoid typing the whole path (and tying the app to that path) by pointing to the file relative to the app's location. 您可以通过指向相对于应用程序位置的文件来避免键入整个路径(并将应用程序绑定到该路径)。

import os
db_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'app2.db'))
engine = create_engine('sqlite://{}'.format(db_path))

Consider using Flask-SQLAlchemy rather than trying to manage the database yourself. 考虑使用Flask-SQLAlchemy而不是尝试自己管理数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM