Flask Connecting the database Mysql+sqlalchemy

Source: Internet
Author: User

Using the Flask framework to link 2 types of databases

----------db.py

#-*-coding:utf-8-*-#Flask Hello World fromFlaskImportFlask fromFlask.ext.mysqlImportMysqlapp= Flask (__name__)" "' # # #链接数据库MySQL版mysql = MySQL () app.config[' mysql_database_user ') = ' root ' app.config[' mysql_database_password '] = ' Root ' app.config[' mysql_database_db ' = ' test ' app.config[' mysql_database_host '] = ' localhost ' mysql.init_app (APP) cursor = Mysql.connect (). Cursor () if __name__ = = ' __main__ ': Cursor.execute ("SELECT * from Db_admin") data = cursor . Fetchone () Print data" "## #SQLAlchemy版__author__='Ghost' fromSQLAlchemyImportCreate_engine, Table, Column, Integer, String, MetaData, ForeignKey, select, text#connecting to a databaseEngine = Create_engine ("Mysql://root:[email Protected]/test?charset=utf8", encoding="Utf-8", echo=True)#get Meta Datametadata =MetaData ()#Defining Tablesuser = Table ('User', metadata, Column ('ID', Integer, primary_key=True), Column ('name', String (20)), Column ('FullName', String (40)) , address= Table ('Address', metadata, Column ('ID', Integer, primary_key=True), Column ('user_id', None, ForeignKey ('user.id')), Column ('Email', String (nullable=),False))#Create a data table that is ignored if the data table existsMetadata.create_all (Engine)#Get database connectionconn =Engine.connect ()" "# # #插入数据i = User.insert () u = dict (name= ' Bob ', Fullname= ' Bobb ') R = Conn.execute (i, **u) # # # #插入多条数据addresses =[{' User_ Id ': 1, ' email ': ' [email protected] '}, {' user_id ': 1, ' email ': ' [email protected] '}, {' user_id ': 2, ' email ': ' [email prote CTED] '}, {' user_id ': 2, ' email ': ' [email protected] '}]a = Address.insert () R = Conn.execute (A, addresses)" "## # #查询多个字段多条数据s =select ([user]) R=Conn.execute (s). Fetchall ()PrintR## # #查询多个字段单条数据s =select ([user]) R=Conn.execute (s). Fetchone ()PrintR## #查询单个字段数据s =Select ([User.c.id,user.c.name]) R=Conn.execute (s). Fetchall ()PrintR## # #多表查询s = Select ([User.c.name, Address.c.email]). WHERE (user.c.id==address.c.user_id) R=Conn.execute (s). Fetchall ()PrintR## # #操作链接查询" "se_sql = [(User.c.fullname + "," + address.c.email). Label (' title ')]wh_sql = and_ (user.c.id = = address.c.u ser_id, User.c.name = = ' Bob ', Or_ (address.c.email.like ('% @aol. com '), ADDRESS.C . Email.like ('% @msn. com '),)) s = Select (Se_sql). where (wh_sql) R = Conn.execute (s). Fetchall () Print R" "## # #原生sqlsql ='SELECT * from user where id=:id and name=:name's=text (SQL) R= Conn.execute (S, id=3, name='Bob'). Fetchall ()PrintR## #排序 grouped paginations =select ([user]). order_by (user.c.id) s=select ([user]). order_by (User.c.id.desc ()) R=Conn.execute (s). Fetchall ()PrintRS= SELECT ([user]). order_by (User.c.id.desc ()). Limit (3). Offset (0)## # Flashbacks take 3R =Conn.execute (s). Fetchall ()PrintR

The above code is the test code, easy to understand, self-test.

Next time to tell you flask-fom form space, involving the knowledge of HTML has a front-end knowledge of the students more understandable.

Flask Connecting the database Mysql+sqlalchemy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.