当前位置: 首页 > article >正文

Python ORM 框架 SQLModel 快速入门教程

创建模型

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

创建表

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)

添加单条数据

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()

添加多条数据

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()

根据名称查询数据

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()

查找所有数据

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 查询

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 查询

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 查询

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()

分页查询

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()

修改数据

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)

删除数据

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()


http://www.kler.cn/a/316472.html

相关文章:

  • 初始JavaEE篇 —— 网络原理---传输层协议:深入理解UDP/TCP
  • 华为管理变革之道:奋斗文化与活力
  • 微信小程序中遇到过的问题
  • PVE虚拟化平台之开启虚拟机IP显示方法
  • linux命令中cp命令-rf与-a的差别
  • 虚幻引擎是什么?
  • 每日一道算法题(二)—快乐数
  • STaR: Bootstrapping Reasoning With Reasoning
  • Android 如何实现搜索功能:本地搜索?数据模型如何设计?数据如何展示和保存?
  • 基于YOLOv8+LSTM的商超扶梯场景下行人安全行为姿态检测识别
  • Python3使用websocket调用http代理IP
  • IP包头分析
  • 【SSM-Day2】创建SpringBoot项目
  • 基于丹摩智算平台-手把手拿下经典目标检测模型 Faster-Rcnn
  • H5响应式的文化传媒娱乐公司HTML网站模板源码
  • Linux入门学习:深刻理解计算机硬件与OS体系
  • saltstack配置管理
  • 深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
  • C++第七节课 运算符重载
  • 航拍房屋检测系统源码分享
  • 对条件语言模型(Conditional Language Model)的目标函数的理解
  • C语言编译四大阶段
  • EasyExcel的基本使用——Java导入Excel数据
  • [C#]winform 使用opencvsharp实现玉米粒计数
  • 基于windows的mysql5.7安装配置教程
  • Vue 实现高级穿梭框 Transfer 封装