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()
相关推荐
不是二师兄的八戒16 分钟前
本地 PHP 和 Java 开发环境 Docker 化与配置开机自启
java·docker·php
nuclear201127 分钟前
使用Python 在Excel中创建和取消数据分组 - 详解
python·excel数据分组·创建excel分组·excel分类汇总·excel嵌套分组·excel大纲级别·取消excel分组
闲暇部落27 分钟前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
爱编程的小生27 分钟前
Easyexcel(2-文件读取)
java·excel
Lucky小小吴42 分钟前
有关django、python版本、sqlite3版本冲突问题
python·django·sqlite
带多刺的玫瑰44 分钟前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
GIS 数据栈1 小时前
每日一书 《基于ArcGIS的Python编程秘笈》
开发语言·python·arcgis
爱分享的码瑞哥1 小时前
Python爬虫中的IP封禁问题及其解决方案
爬虫·python·tcp/ip
计算机毕设指导61 小时前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
Gu Gu Study1 小时前
枚举与lambda表达式,枚举实现单例模式为什么是安全的,lambda表达式与函数式接口的小九九~
java·开发语言