Officially, Flask-SQLAlchemy is recommended, but I personally don’t recommend it here. Because general back-end programmers prefer to use native SQL language to deal with database problems, a more important reason is that after the project is online, when dealing with some operation and maintenance problems, if you can view the SQL statement can find problems faster. Second, Flask-SQLAlchemy encapsulates a layer on top of SQLAlchemy, which is definitely less efficient from a compilation point of view (the impact of efficiency may be minimal).

For these reasons, I recommend using SQLAlchemy for development.

The following two methods are not limited to flask, but are Python implementations of database operations.

The first way (for queries):

SQL Alchemy import create_engine from SQLAlchemy import create_engine,text # SQL Alchemy import create_engine,text # 'root' PWD = 'PWD dburl =' mysql + mysqldb: / / {}, {} @ {}, {} / {} '. The format (username and PWD, hostname, port, database) # create a database connection object engine = create_engine(dburl,echo=True) with engine.connect() as con: rs = con.execute('SELECT 1') # con.execute(text("select 1 ")) for row in rs: print row

The second way (for adding, deleting or changing, there are transactions):

Sqlalchemy import create_engine,text from sqlalchemy. ORM import sessionmaker #  '3306' database = 'dbname' username = 'root' pwd = 'pwd' dburl = 'mysql + mysqldb: / / {}, {} @ {}, {} / {}'. The format (username and PWD, hostname, port, database) engine = # to create a database connection object create_engine(dburl,echo=True) Session = sessionmaker(bind=engine) session = Session() session.execute("insert test values ('abc','123')") session.commit() session.close()