zoukankan      html  css  js  c++  java
  • python学习教程(九)sqlalchemy框架的modern映射

    首先写一个modern.py文件,


    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String
    from sqlalchemy.engine import create_engine
    
    '''the connection to the database'''
    engine = create_engine("mysql://root:1933jld1945js@localhost/test",isolation_level="READ UNCOMMITTED") 
    '''the construct of the database table'''
    Base = declarative_base()
    
    class People():
        id = Column(Integer, primary_key=True)
        name = Column(String(16))
        gender = Column(String(16))
        age = Column(String(16))
    
    class User(Base,People):
        __tablename__ = 'users'
    
        fullname = Column(String(16))
        password = Column(String(16))
    
    class AdvancedUser(Base,People):
        __tablename__ = 'advanceduser'
        
        fullname = Column(String(16))
        password = Column(String(16))
        email = Column(String(16))
        
    class Teacher(Base,People):
        __tablename__ = 'teacher'
        
        phone = Column(String(16))
        
    Base.metadata.create_all(engine)
    


    然后写一个control.py文件,内容如下:

    from sqlalchemy.orm.session import sessionmaker
    from modern import engine,User,AdvancedUser,Teacher
    
    user = User()
    user.name = 'username'
    user.gender = 'male'
    au = AdvancedUser()
    au.name = 'auname'
    au.gender = 'female'
    teacher = Teacher()
    teacher.name = 'wangdi'
    teacher.gender = 'female'
    teacher.phone = '15264397652'
    Session = sessionmaker()
    Session.configure(bind=engine)
    session = Session()
    session.add(user)
    session.add(au)
    session.add(teacher)
    session.flush()
    session.commit()


    运行control.py可以测试代码。

  • 相关阅读:
    C++ Call C# COM
    C ++ / CLI 语法
    C#调用C++ DLL动态库的两种方式
    Linux下的JMeter部署及使用
    同时处理多请求(带超时时间)
    同时处理多请求
    curl命令
    mysql命令
    SpringBoot配置文件敏感信息加密-jasypt
    spring boot升级到2.x的坑
  • 原文地址:https://www.cnblogs.com/pangblog/p/3313400.html
Copyright © 2011-2022 走看看