Python ORM Framework SQLAlchemy Simple Application (Database operations)

Source: Internet
Author: User

#_ *_coding:utf-8_*_ "To create a SQLAlchemy application basic procedure 1, create a connection (and a relational database to create a connection) 2, declare a mapping file (Python-to-class and database tables do a one-to-one mapping, This allows you to manipulate the tables in the database through the classes in Python 3, create the schema (can create the table) 4, initialize the map instance 5, create session 6, persist the instance object ' from sqlalchemy import  create_engine# Create Connection Engine = create_engine (' Mysql://root:1qaz#[email protected]:3306/student ',  echo=true) "' parameter description:mysql  indicates that the database to be connected is a MySQL database root  means log in to the MySQL database with the root account 1qaz#edc  The password for the root account that represents the connection database @  is a symbol that specifies the format 192.168.89.1.101:3306  represents the database's connection address and port 3306soms  represents the name of the database that will be connected echo= true  indicates whether to print the SQL statement at execution time, false to not print "' #声明映射文件from  sqlalchemy.ext.declarative import  declarative_base  #引入了declarative_base类Base  = declarative_base ()   #实例化了一个declarative_ Base instance from sqlalchemy import column, integer, string  #引入类Column,  Integer,  string class Class user (base):  #创建自己的实例类   name user  inherit Base class     __ tablename__ =  ' users '   #这个属性, which indicates that this class and the Users table in the database are mapped &NBSP;&NBSP;&NBSp; id = column (integer, primary_key=true)   #这个属性表示id在数据库表中代表着一列, integer means integers, and ID is primary key     name = column (String)   #这个属性表示在数据库表中也是一列, its type is string     passwd = column (String)   #这个属性表示在数据库表中也是一列, its type is string     Def __repr__ (self):  #定义了一个方法, the purpose is to make the user class more image representation, not much use          return  "<user (name= '%s ',  name= '%s ',  password= '%s ') >"  %  (self.name,  SELF.NAME,&NBSP;SELF.PASSWD) #创建模式, create a database table, table name: UsersUser.metadata.create_all (Engine) #创建会话 (session) "Create a" from  sqlalchemy.orm import sessionmaker  #引入sessionmaker这个函数Session  = sessionmaker ( Bind=engine)   #创建session对话, this Session allows you to perform some operations on the database Session = session ()   #绑定 # to persist an instance object Ed_user  = user (id=2,name= ' Tantianran ',  passwd= ' 1qaz#edc ')   #向数据库表中到列插入数据session. Add (ed_user)   #将这个实例添加到session中session. Commit ()   #提交 


Execution Result:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8E/B2/wKiom1jJHl6yYcdxAAIbcFYWbmI008.png-wh_500x0-wm_ 3-wmp_4-s_2213477610.png "style=" Float:none; "title=" 1.png "alt=" Wkiom1jjhl6yycdxaaibcfywbmi008.png-wh_50 "/>

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8E/B2/wKiom1jJHl6BNK5ZAABOW5cLzls503.png-wh_500x0-wm_ 3-wmp_4-s_2256356209.png "style=" Float:none; "title=" 2.png "alt=" Wkiom1jjhl6bnk5zaabow5clzls503.png-wh_50 "/>


This article is from the "Operation and maintenance of AC Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1906971

Python ORM Framework SQLAlchemy Simple Application (Database operations)

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.