Python ORM Framework SQLAlchemy example code for simple applications (database operations)

Source: Internet
Author: User
This article details the instance code of the Python ORM Framework SQLAlchemy Simple Application (Database operation)

#_ *_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#edc@192.168.89.101:3306/student ', echo=true) ' parameter description: MySQL Indicates that the database to be connected is the MySQL database root means log in to the MySQL database with the root account 1qaz#edc the password for the root account of the connection database is a symbol, the specified format 192.168.89.1.101:3306 Indicates that the database's connection address and port 3306soms indicate 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实例 from SQLAlchemy Import Column, Integer, String #引入类Column, Integer, String class User (Base): #创建自己的实例类 named user inherits base class __tablename__ =    The ' users ' #这个属性, which indicates that this class and the Users table in the database are mapped with the id = Column (integer, Primary_key=true) #这个属性表示id在数据库表中代表着一列, the integer is an integer, and the ID is the primary key     Name = Column (string) #这个属性表示在数据库表中也是一列, which is of type string passwd = Column (String) #这个属性表示在数据库表中也是一列, which is of type stringdef __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, 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 () #绑定 #持久化一个实例对象ed_user = User (id=2,name= ' Tantianran ', passwd= ' 1qaz#edc ') # Inserts a data session.add (Ed_user) #将这个实例添加到session中session into a database table into a column. Commit () #提交

Execution Result:

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.