常用数据类型
- integer: 整形
- Float: 浮点类型
- Boolean:传递True/False进去
- DECIMAL:顶点类型
- enum: 枚举类型
- Date:传递dateting.date()进去
- DateTime:传递dateting.datetime()进去
- Time:传递datetime.time()进去
- String:字符类型,使用时需要指定长度,区别于text类型
- Text:文本类型
- LONGTEXT:长文本类型
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float, DECIMAL, Boolean, Enum
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Date, DateTime, Time
from datetime import datetime, date, time
from sqlalchemy.dialects.mysql import LONGTEXT
HOSTNAME = '127.0.0.1'
DATABASE = 'demo0417'
PORT = 3306
USERNAME = 'root'
PASSWORD = 'root'
DB_URL = 'mysql+mysqlconnector://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
engine = create_engine(DB_URL)
Base = declarative_base(engine)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(60))
price = Column(DECIMAL(20,6))
is_delete = Column(Boolean)
gender = Column(Enum('男', '女'))
create_time = Column(DateTime)
content = Column(LONGTEXT)
Base.metadata.drop_all()
Base.metadata.create_all()
Session = sessionmaker(bind=engine)
session = Session()
user = User(name='happyy', price=88.9876, is_delete=True, gender='女', create_time=datetime(2020, 5, 1, 9, 2), content='ssssshappyyyyppha')
session.add(user)
session.commit()
Column常用参数
- default:默认值
- nullable:是否可空
- primary_key :是否为主键
- unique:是否唯一
- autoincrement:是否自动增长
- onupdate:更新的时候执行的函数
- name:该属性在数据库中的字段映射
update_time = Column(DateTime, onupdate=datetime.now())
user = session.query(User).first()
print(user.name)
user.name = 'hahaha'
session.commit()
name = Column('fullname', String(60))
Base.metadata.create_all()
Session = sessionmaker(bind=engine)
session = Session()
user = User(name='happyy', price=88.9876, is_delete=True, gender='女', create_time=datetime(2020, 5, 1, 9, 2), content='ssssshappyyyyppha')
session.add(user)
session.commit()
query可用参数
- 模型对象。 指定查找这个模型中所有的对象
- 模型中的属性。 可以指定只查找某个模型的其中几个属性
- 聚合函数。
func.count 统计行的数量 - func.avg: 求平均值
- func.max: 求最大值
- func.min : 求最小值
- func.sum: 求和。
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.orm import sessionmaker
from sqlalchemy import func
import random
HOSTNAME = '127.0.0.1'
DATABASE = 'demo0417'
PORT = 3306
USERNAME = 'root'
PASSWORD = 'root'
DB_URL = 'mysql+mysqlconnector://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
engine = create_engine(DB_URL)
Base = declarative_base(engine)
class Article(Base):
__tablename__ = 'article1'
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(50), nullable=False)
price = Column(Float, nullable=False)
def __str__(self):
return "Article(title:{},price:{})".format(self.title, self.price)
Base.metadata.create_all()
Session = sessionmaker(bind=engine)
session = Session()
result = session.query(func.sum(Article.price)).first()
print(result)