zoukankan      html  css  js  c++  java
  • sqlalchemy的常用字段

    #encoding: utf-8
    
    from sqlalchemy import create_engine,Column,Integer,String,
        Float,Boolean,DECIMAL,Enum,Date,DateTime,Time,Text
    from sqlalchemy.dialects.mysql import LONGTEXT
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker
    
    #python2没有枚举
    #python3有枚举
    import enum
    
    
    HOSTNAME = '127.0.0.1'
    
    PORT = 3306
    
    DATABASE = 'first_sqlalchemy'
    
    USERNAME = 'root'
    
    PASSWORD = '123456'
    
    #dialect+driver://username:password@host:port/database
    DB_URI = "mysql+pymysql://{username}:{password}@{host}:{port}/" 
             "{db}?charset=utf8".format(username=USERNAME,password=PASSWORD,host=HOSTNAME,port=PORT,db=DATABASE)
    
    engine = create_engine(DB_URI)
    
    Base = declarative_base(engine)
    
    # Session = sessionmaker(engine)
    # session = Session()
    
    session = sessionmaker(engine)() #Session(**local_kw)
    
    
    #定义一个自己的标签枚举类
    class TagEnum(enum.Enum):
        python = 'python'
        flask = 'flask'
        django = 'django'
    
    
    class Article(Base):
        __tablename__ = 'article'
        id = Column(Integer,primary_key=True,autoincrement=True)
        # price = Column(Float)
        # is_delete = Column(Boolean)
        # price = Column(DECIMAL(10,4))
    
        # tag = Column(Enum('python','flask','django'))#原生
    
        #自定义枚举类怎么映射数据库
        # tag = Column(Enum(TagEnum))
    
        # create_time = Column(Date)
    
        # create_time = Column(DateTime)
    
        # create_time = Column(Time)
    
        # title = Column(String(50))
    
        # content = Column(Text)
    
        content_long = Column(LONGTEXT)
    #删除类到数据库的映射数据
    Base.metadata.drop_all()
    
    Base.metadata.create_all()
    
    #增加一条数据
    
    # article = Article(price=10.293)
    # session.add(article)
    # session.commit()
    
    #在单独增加一条数据
    # article = Article(price=10.29334)
    # session.add(article)
    # session.commit()
    
    #验证Boolean类型
    # article = Article(is_delete=True)
    # session.add(article)
    # session.commit()
    
    
    #验证DEcimal类型
    # articel = Article(price = 999999.9999)
    # session.add(articel)
    # session.commit()
    
    
    #验证Enum枚举类型
    # article = Article(tag = 'python')
    # session.add(article)
    # session.commit()
    
    #验证自定义Enum的传递
    # article = Article(tag = TagEnum.flask)
    # session.add(article)
    # session.commit()
    
    #验证Date类型
    # from datetime import date
    #
    # article = Article(create_time=date(2018,9,22))
    # session.add(article)
    # session.commit()
    
    #验证Datetime类型
    # from datetime import datetime
    #
    # article = Article(create_time = datetime.now())
    #
    # session.add(article)
    # session.commit()
    
    #验证Time类型
    # from datetime import datetime
    # from datetime import time
    #
    # #datetime.now().time()取时间
    # article = Article(create_time = datetime.now().time())
    #
    # session.add(article)
    #
    # session.commit()
    
    
    #验证String类型
    
    # article = Article(title = '文章标题')
    # session.add(article)
    # session.commit()
    
    #验证Text类型
    # article = Article(content = '文章标题aaaaaa')
    # session.add(article)
    # session.commit()
    
    #验证LONGTEXT类型
    article = Article(content_long = '文章标题aaaaaa')
    session.add(article)
    session.commit()
  • 相关阅读:
    CentOS+nginx+uwsgi+Python 多站点环境搭建
    nginx实现负载均衡
    高性能Mysql主从架构的复制原理及配置详解
    centos 安装php
    Java知识总结-6
    Java知识总结-5
    Java知识总结-4
    Java知识总结-3
    Java知识总结-2
    java知识总结-1
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/9691259.html
Copyright © 2011-2022 走看看