I'm trying to create a Web application using flask and flask-sqlalchemy, i have the following code
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.
I've tried adding the port number, removing the python connector but nothing works. 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.
SQLAlchemy will not actually create the database for you. 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.