From Flask_sqlalchemy import sqlalchemyimport mysqlimport Mysqldbapp = Flask (__name__) app.config[' Sqlalchemy_database _uri '] = ' mysql://root:[email protected]:3306/testbase?charset=utf8mb4 ' app.config[' Sqlalchemy_commit_on_teardown ' ] = Truedb = SQLAlchemy (APP) class User (db. Model): __tablename__ = ' users ' #定义数据库中的表名 id = db. Column (db. Integer, Primary_key = True) #主键 username = db. Column (db. String (+), unique = True) #用户名, duplicate password = db is not allowed. Column (db. String (nullable = False) #密码, not allowed for null if __name__ = = ' __main__ ': app.run ()
Refer to the content on the Internet using Flask-sqlalchemy to connect to MySQL
The process is not smooth, encountered a variety of problems
1, 1049, "Unknown database"
SQLAlchemy cannot help you create a database, you must connect to an already established database.
Go to command line mysql-> CREATE DATABASE testbase
Build the database.
2, 1146, "Table ' performance_schema.session_variables ' doesn ' t exist"
Resolve after adding "import MySQLdb" and "Import MySQL". specifically to join which no test
Problems connecting to MySQL using Flask-sqlalchemy