#-*-coding:utf-8-*-ImportSQLAlchemy fromSQLAlchemyImportCreate_engine fromSqlalchemy.ext.declarativeImportDeclarative_base fromSQLAlchemyImportColumn, Integer, String fromSqlalchemy.ormImportSessionmakerengine= Create_engine ('Mysql+mysqlconnector://plan:[email Protected]/plan') Base= Declarative_base ()#generating an ORM base classclassUser (Base): #进行映射, CREATE table structure, models, three entries, id name password__tablename__='User' #Table nameid = Column (Integer, primary_key=True) name= Column (String (32)) Password= Column (String (64) ) Base.metadata.create_all (engine)#CREATE TABLE StructureSession_class = Sessionmaker (bind=engine)#create a session with the database sessions class, note that this is a class that is returned to the session, not an instanceSession = Session_class ()#Generating Session Instances#session.close () #关闭连接 this last#Insert StartPrint('Insert Start') User_obj= User (name="Jack", password="Jack")#build the data object you want to createPrint(User_obj.name, User_obj.id)#No object has been created at this time, do not believe you print the ID found or noneSession.add (user_obj)#Add the data object you want to create to this session, and create a unifiedPrint(User_obj.name, User_obj.id)#This is still not created yet .Session.commit ()#Now this is the unified submission, create dataPrint('Submit Data')Print(User_obj.name, User_obj.id)#submitted, it will be placed in the database. #Insert EndPrint('Insert End')#Query StartPrint('Query Start') My_user= Session.query (User). Filter_by (name="Jack"). First ()#Find the Name=jackPrint(My_user)#this is an object.Print(My_user.id, My_user.name, My_user.password)#End of QueryPrint('End of Query')#Delete StartPrint('Delete Start')#First Insert one, can be deleted. User_obj = User (name="Rose", password="Jack") Session.add (user_obj) session.commit ()#find the object you want to delete.Del_user = session.query (user). filter_by (name="Rose"). First ()Print(Del_user.name) session.delete (del_user)#Delete commandSession.commit ()#Commit DeleteFind_user = session.query (user). All ()#querying the entire contents of a table forIinchFind_user:Print(I.id, I.name)#Delete End#Modify StartPrint('Modify Start') My_user= Session.query (User). Filter_by (name="Jack"). First ()#Find the first name is Jack's, build the object. My_user.name ="Jackadam" #to name an object variabletemp_id = My_user.id#Assign the ID of this object to the TEMP variable temp_idSession.commit ()#Submit ChangesMy_user2 = Session.query (User). filter_by (id=temp_id). First ()#according to the temporary variable ID, re-query the name is modified. Print(My_user2.name)#Modify EndPrint('Modify End')#Multi-Criteria QueryOBJS = Session.query (User). Filter (User.ID > 0). filter (User.ID < 7). All ()#Multi-Criteria Query#StatisticsPrint('Statistics') Count_num= Session.query (User). Filter (User.name.like ("ja%") . Count ()Print(Count_num)#Statistics#GroupingPrint('Grouping') fromSQLAlchemyImportfuncPrint(Session.query (Func.count (user.name), User.Name). group_by (User.Name). All ())#GroupingSession.close ()#Finally, I don't forget close ." "orady_bydesc ascending ASC Descending" "
The notes are very detailed, not written.
SQLAlchemy (b) Simple Connection example