Python--orm Example

Source: Internet
Author: User

Tag: Print OCA string reg session generates SQL Change Sina

Build Table Insert Data:

import sqlalchemyfrom sqlalchemy Import Create_enginefrom Sqlalchemy.ext.declarative Import declarative_basefrom sqlalchemy import Column, Integer,string,datefrom                       Sqlalchemy.orm Import sessionmakerengine = Create_engine ("Mysql+pymysql://tim:[email protected]/test", encoding = "Utf-8", echo=true) Base = Declarative_base () class User (Base): __tablename__= "user" id = Column (in teger,primary_key=true) name = Column (string ()) Password = column (string) Base.metadata.create_all (engine) #创建 Table Structure Session_class = Sessionmaker (bind=engine) #创建与数据库的会话session class, note that this is a class that is returned to the session, not the instance session = Session_class ( #生成session实例user_obj = User (Name= ' Tim ', password= ' sina4433 ') #生成你要创建的数据对象user_obj2 = user (name = ' Jack ', password= ' ABC Print (user_obj.name,user_obj.id) #此时还没创建对象, do not believe in printing the ID, or Nonesession.add (user_obj) # To add the data object to be created in this session, For a moment unified create Session.add (USER_OBJ2) print (user_obj,user_obj.id) #此时也依然还没创建Session. Commit () 

Inquire:

Import sqlalchemyfrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import Declarative_basefrom SQLAlchemy import Column, integer,string,datefrom sqlalchemy.orm Import sessionmakerengine = Create_engine ("mysql+ Pymysql://tim:[email protected]/test ", encoding =" Utf-8 ") Base = Declarative_base () class User (B ASE): __tablename__= "user" id = column (integer,primary_key=true) name = column (String) password = column ( String (+)) def __repr__ (self): return "<%s name:%s>"% (self.id,self.name) Base.metadata.create_all (engine ) #创建表结构Session_class = Sessionmaker (bind=engine) #创建与数据库的会话session class, note that this is a category that is returned to the session, not an instance session = Session_ Class () #生成session实例 # user_obj = User (Name= ' Tim ', password= ' sina4433 ') #生成你要创建的数据对象 # user_obj2 = User (name = ' Jack '), password= ' abc ') # Print (user_obj.name,user_obj.id) #此时还没创建对象, do not believe to print the ID, or none## session.add (user_obj) # Add the data object you want to create to this Session, and create a unified # Session.add (USER_OBJ2) # print (User_obj,user_obj.id) #此时也依然还没创建 # session.commit () # data = Session.query (User). Filter_by (). All () #结果是一个列表 # Print (data) # data = Sessi On.query (user) filter_by (name= ' Tim '). All () #结果是一个列表 # Print (data[0].id) # data = Session.query (User). filter_by (id=1). All () # print (data) # data = session.query (user). Filter (user.id>1). All () # print (data) # data = Session.query (User). Filter (user.id>1). Filter (USER.ID&LT;4). All () # print (data) data = Session.query (User). Filter (user.id>1). Filter (USER.ID&LT;4). First () print (data) # #更改数据 # data.name = "Zhang" # data.password = "Shit" # Session.commit () # My_user = Session.query (user) filter_by (id=1). First () # my_user.name = ' Jack ' # # fake_user = User (name= ' Rain ', password= ' 123456 ') # # Session.add (fake_user) # # Print (session.query (user). Filter (User.name.in_ ([' Jack ', ' rain ']). All ()) # # Session.rollback () # # print (Session.query (User). Filter (User.name.in_ ([' Jack ', ' rain ']). All ())

Multiple table connections:

Import sqlalchemyfrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import Declarative_basefrom SQLAlchemy import Column, integer,string,enum,foreignkey,datefrom sqlalchemy.orm import Sessionmaker, Relationshipengine = Create_engine ("mysql+pymysql://tim:[email protected]/test", encoding = "UT F-8 ") Base = Declarative_base () class Student (Base): __tablename__=" Student "id = Column (integer,primary_key=true,aut oincrement=true) name = Column (String (+), nullable=true) register_date = column (date,nullable=true) #加个关联关系 de F __repr__ (self): return "<%s name:%s>"% (Self.id,self.name) class Studyrecord (Base): __tablename__ = "Study Record "id = column (integer,primary_key=true,autoincrement=true) day = column (Integer) status = Column (String (32) ) stu_id = Column (Integer,foreignkey (student.id)) #加个关联关系 Student = Relationship ("Student", backref= "My_study_reco Rd ") def __repr__ (self): Return ' <%s day:%s status:%s> "% (self.student.name,self.day,self.status) Base.metadata.create_all (engine) #创建表结构Session_class = Sessionmaker (bind=engine) #创建与数据库的会话session class, notice that this is a class that is returned to the session, not an instance session = Session_class () #生成session实例 # RET = Session.query (Student). Join (Study_record). All () #这种方式需要主外键关联 # print (ret) # ret = Session.query (Student,studyrecord) . Filter (STUDENT.ID==STUDYRECORD.STU_ID). All () # print (ret) # S1 = Student (name= "Alex", register_date= ' 2013-02-01 ') # s2 = Student (name= "Jack", register_date= ' 2014-04-01 ') # s3 = Student (name= "Gao", register_date= ' 2013-10-01 ') # S4 = Student ( Name= "Wang", register_date= ' 2011-11-01 ') # # study_obj1 = Studyrecord (day=1,status= "YES", stu_id=1) # study_obj2 = Studyrecord (day=2,status= "NO", stu_id=1) # study_obj3 = Studyrecord (day=3,status= "YES", stu_id=1) # Study_obj4 = Studyrecord (day=1,status= "YES", stu_id=2) # # Session.add_all ([S1,s2,s3,s4,study_obj1,study_obj2,study_obj3,study_ OBJ4]) # session.commit () #查出Alex上了几节课stu_obj = Session.query (Student). Filter (student.name== ' Alex '). First () print (Stu_obj.my_study_record) 

Python--orm Example

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.