[英]flask sqlalchemy unknown database error
I'm trying to create a Web application using flask and flask-sqlalchemy, i have the following code 我正在尝试使用flask和flask-sqlalchemy创建一个Web应用程序,我有以下代码
from flask.ext.sqlalchemy import SQLAlchemy
from flask import Flask
app= Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']= 'mysql+mysqldb://root:presario@127.0.0.1/testdb'
db = SQLAlchemy(app)
When i run db.create_all() from command line, i get unknown database 'testdb' error I'm working on ubuntu but the code above works on my windows machine. 当我从命令行运行db.create_all()时,我得到未知数据库'testdb'错误我正在使用ubuntu,但上面的代码在我的Windows机器上运行。
I've tried adding the port number, removing the python connector but nothing works. 我已经尝试添加端口号,删除python连接器但没有任何作用。 below is the stack trace
下面是堆栈跟踪
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (OperationalError) (1049, "Unknown database 'testdb'") None None
I would suggest that the database 'testdb' does not exist. 我建议数据库'testdb'不存在。
SQLAlchemy will not actually create the database for you. SQLAlchemy实际上不会为您创建数据库。 You have to connect to an existing database.
您必须连接到现有数据库。 It will then create all the tables.
然后它将创建所有表。
Joe 乔
You can create DB like this: 您可以像这样创建数据库:
url = 'mysql://%s:%s@%s' % (USER, PASSWORD, HOST)
engine = sqlalchemy.create_engine(url) # connect to server
create_str = "CREATE DATABASE IF NOT EXISTS %s ;" % (DATABASE)
engine.execute(create_str)
engine.execute("USE location;")
db.create_all()
db.session.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.