Python flask use experience

Python flask uses lessons learned to summarize


During this time, I started to contact flask, encountered some problems in the middle, and went through writing detours. This blog is used to explain the pits I stepped on during this period.


Question 1: When using SQLAlchemy to manage MySQL database, an error is reported when creating a db instance, prompting Import MySQLdb error

It should be noted that when using SQLAlchemy, MySQL database and MySQLdb must be installed first, because SQLAlchemy will call the MySQLdb module. You can read this blog to install the MySQLdb module under Windows. If you install it under Linux, you can refer to my blog.

Question 2: Also when using SQLAlchemy, an OperationalError: (_mysql_exceptions.OperationalError) (1045, “Access denied for user'ODBC'@'localhost' (using password: NO)” is displayed when executing db.create_all() to create a data table.
Analysis shows that the permissions are not enough, and the operating database is not authorized in the database.
You can use a method similar to the following, for example, if the database is named test, the authorization statement is


grant select ,insert,update,delete,lock tables on test.* to [email protected] identified by 'password';

Fill in your user name in the username field above, and fill in your password when connecting to the password field.

Question 3: When using *SQLAlchemy, an error occurs when executing db.create_all(), prompt: OperationalError: (_mysql_exceptions.OperationalError) (1049, “Unknown database'flasky'”)

Analysis: The error indicates that the unknown database "flasky"*, because the database named flasky does not exist in the MySQL database. Note that before creating a table, you must ensure that the required database has been created in MySQL. The name of this database is set when the flask application is created


app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:[email protected]/flasky'

Flasky at the end of the above is the name of the database used. This must be established in the MySQL database.
Therefore, the reasonable sequence is: first create the flasky database in MySQL, then authorize, and then define the type in the flask application and create the data table. The
above process is similar to the following operations:

First define the database in the database and authorize

create database flasky;
grant select,insert,update,delete,lock tables on flasky.* to [email protected] identified by 'password';

Next, define the ORM in the flask code, create an instance of SQLAlchemy and use it when necessary

from flask.ext.sqlalchemy import SQLAlchemy

app.config['SQLALCHEMY_DATABASE_URI']='mysql://username:[email protected]/flasky' #使用MySQLdb数据库
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']=True #设置为自动提交


class Role(db.Model):

def repr (self):
return'<Role %r>'

class User(db.Model):
tablename = "users"
role_id= db.Column(db.Integer,db.ForeignKey(''))

def __repr__(self):
    return '<User %r>'%self.username

然后可以在python shell下执行db.create_all()方法在数据库中创建两个数据表。

问题四:在使用模板引擎时正文采用中文会出现utf8 decode error的错误出现

原因是模板文件的编码不对,最好采用utf8无BOM的格式。在notepad++ 中默认编码是ANSI,需要改过来。