sqlaclchemy 常用数据类型 常用字段限制

demo4.py

from sqlalchemy import (
    create_engine,
    Integer,
    String,
    Column,
    Float,
    Boolean,
    DECIMAL,
    DateTime,
    DATE,
    Time,
    Text,
    Enum

)
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker#sessionmaker 是基类

HOSTNAME = '127.0.0.1'#数据库地址
PORT     = '3306'#端口号
DATABASE = 'dasheng'#数据库名字
USERNAME = 'root'#用户名
PASSWORD = '123456'#密码
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE)


#创建数据库引擎
engine = create_engine(DB_URI)
Base = declarative_base(engine)
session = sessionmaker(engine)()
import enum
class TagNum(enum.Enum):
    China = '国产'
    japan = '日韩'
    America = '欧美'

from datetime import datetime
class Article(Base):
    __tablename__ = 'article'
    id = Column(Integer,primary_key=True,autoincrement=True)
    price = Column(DECIMAL(10,4))
    is_delete = Column(Boolean)
    create_time = Column(DateTime,onupdate=datetime.now,default=datetime.now)
    title = Column(String(50),unique=True,nullable=False)
    # content = Column(Text)
    content = Column(LONGTEXT)
    tag = Column(Enum(TagNum))


Base.metadata.drop_all()
Base.metadata.create_all()

你可能感兴趣的:(sqlaclchemy 常用数据类型 常用字段限制)