Mysql-sqlalchemy-orm Frame

Source: Internet
Author: User

Import sqlalchemyfrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import declarative_base# Column import from SQLAlchemy import Column, Integer, stringfrom sqlalchemy.orm import sessionmaker# the cursor package # Connection Database engine = Create _engine ("Mysql+pymysql://root:[email protected]/oldboy", encoding= ' Utf-8 ', echo=true) #echo =true is to put all the letters is printed on the console base = Declarative_base () #生成orm基类class User (base): __tablename__ = ' user ' #表名 id = Column (Integer, Primary_ key=true) name = Column (string ()) Password = column (string) Base.metadata.create_all (engine) #创建表结构 "Insert data operation ' S Ession_class = Sessionmaker (bind=engine) #创建与数据库的会话session class, notice that the Session returned is Classsession =session_class () # Generating session instances is equivalent to cursor cursor user_obj = User (name= "Dapangpang", password= "521521") #生成你要创建的数据对象print (User_obj.name,user_ obj.id) #此时还没创建对象Session. Add (user_obj) #把药创建的数据对象添加到这个session里, one will agree to create print (user_obj.name,user_obj.id) # You still haven't created the object Session.commit () #现此才统一提交 to create the data

Mysql-sqlalchemy-orm Frame

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.