Python ORM 框架 SQLModel 快速入门教程

创建模型

python 复制代码
import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None

创建表

python 复制代码
import sqlmodel
import typing



class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None

db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

添加单条数据

python 复制代码
from tkinter.font import names

import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs = Hero(name="张三", real_name="张三", age=33)
    session.add(zs)
    session.commit()

添加多条数据

python 复制代码
from tkinter.font import names

import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    session.commit()

根据名称查询数据

python 复制代码
from tkinter.font import names

import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 查找
    query = sqlmodel.select(Hero).where(Hero.name=="张三1")
    hero = session.exec(query).first()
    print(hero)

    session.commit()

查找所有数据

python 复制代码
from tkinter.font import names

import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 查找
    query = sqlmodel.select(Hero)
    heros = session.exec(query)
    print(list(heros))

    session.commit()

and 查询

python 复制代码
from tkinter.font import names

import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 查找
    query = sqlmodel.select(Hero).where(Hero.real_name=="张三", Hero.name=="张三3")
    hero = session.exec(query).first()
    print(hero)

    session.commit()

or 查询

python 复制代码
import sqlmodel
import typing

class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 查找
    query = sqlmodel.select(Hero).where(sqlmodel.or_(
        Hero.name == "张三1",
        Hero.name == "张三2",
    ))
    heros = session.exec(query)
    print(list(heros))

    session.commit()

根据 ID 查询

python 复制代码
import sqlmodel
import typing

class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 查找
    hero  =session.get(Hero, 1)
    print(hero)

    session.commit()

分页查询

python 复制代码
import sqlmodel
import typing

class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 查找
    query = sqlmodel.select(Hero).offset(0).limit(2)
    heros = session.exec(query)
    print(list(heros))

    session.commit()

修改数据

python 复制代码
import sqlmodel
import typing


class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 修改
    hero = session.get(Hero, 1)
    hero.name = "李四"
    session.commit()

with sqlmodel.Session(engine) as session:
    # 修改
    hero = session.get(Hero, 1)
    hero.name = "李四"
    session.commit()
    session.refresh(hero)

删除数据

python 复制代码
import sqlmodel
import typing



class Hero(sqlmodel.SQLModel, table=True):
    id: typing.Optional[int] = sqlmodel.Field(default=None, primary_key=True)
    name: str
    real_name: str
    age: typing.Optional[int] = None


db_url = "mysql+pymysql://root:zhangdapeng520@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4"
engine = sqlmodel.create_engine(db_url, echo=True)

sqlmodel.SQLModel.metadata.drop_all(engine)
sqlmodel.SQLModel.metadata.create_all(engine)

# add
with sqlmodel.Session(engine) as session:
    zs1 = Hero(name="张三1", real_name="张三", age=33)
    session.add(zs1)
    zs2 = Hero(name="张三2", real_name="张三", age=33)
    session.add(zs2)
    zs3 = Hero(name="张三3", real_name="张三", age=33)
    session.add(zs3)

    # 修改
    hero = session.get(Hero, 1)
    hero.name = "李四"
    session.commit()

with sqlmodel.Session(engine) as session:
    # 删除
    hero = session.get(Hero, 1)
    session.delete(hero)
    session.commit()
相关推荐
小池先生3 分钟前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端
CodeClimb6 分钟前
【华为OD-E卷-木板 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
odng9 分钟前
IDEA自己常用的几个快捷方式(自己的习惯)
java·ide·intellij-idea
夜幕龙13 分钟前
iDP3复现代码数据预处理全流程(二)——vis_dataset.py
人工智能·python·机器人
CT随17 分钟前
Redis内存碎片详解
java·开发语言
brrdg_sefg26 分钟前
gitlab代码推送
java
晚夜微雨问海棠呀1 小时前
长沙景区数据分析项目实现
开发语言·python·信息可视化
hanbarger1 小时前
mybatis框架——缓存,分页
java·spring·mybatis
cdut_suye1 小时前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
苹果醋31 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx