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

Python国产新 ORM 框架 fastzdp_sqlmodel 快速入门教程

创建模型

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


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

创建表

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


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


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)

初始化表

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


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


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

添加数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

u = User(name="张三")
fasm.add(engine, u)

查询所有数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三"))
fasm.add(engine, User(name="李四"))

users = fasm.get_all(engine,User)
print(users)

根据字典查询数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三"))
fasm.add(engine, User(name="李四"))

query = {"name":"张三"}
users = fasm.get_by_dict(engine,User, query)
print(users)

根据字典实现 or 查询

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三"))
fasm.add(engine, User(name="李四"))

query = {"name": {"=": "张三", "==": "李四"}}
users = fasm.get_by_dict_or(engine, User, query)
print(users)

根据字典实现 and 查询

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))

query = {"age": {">": 30, "<": 40}}
users = fasm.get_by_dict_and(engine, User, query)
print(users)

根据ID 查询

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))

u = fasm.get(engine,User,1)
print(u)

更新数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))

u = fasm.get(engine, User, 1)
print("xxxxxx", u)

data = {"name": "张三 333"}
fasm.update(engine, u, data)

u = fasm.get(engine, User, 1)
print("=======", u)

根据 ID 更新数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))


data = {"name": "张三 333"}
fasm.update_id(engine, User,1, data)

u = fasm.get(engine, User, 1)
print("=======", u)

删除数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))


u = fasm.get(engine,User,1)
fasm.delete(engine,u)

根据 ID 删除数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))


fasm.delete_id(engine,User,1)

根据分页查询数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))
fasm.add(engine, User(name="王五", age=33))


users = fasm. get_page(engine,User,page=1,size=2)
print(users)

获取第一条数据

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))
fasm.add(engine, User(name="王五", age=33))


u = fasm.get_first(engine,User,{"name":"张三"})
print(u)

判断数据是否存在

from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasm


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    age: int


engine = fasm.get_engine(password="zhangdapeng520", database="fastzdp_sqlmodel", echo=True)
fasm.init_table(engine)

fasm.add(engine, User(name="张三", age=22))
fasm.add(engine, User(name="李四", age=33))
fasm.add(engine, User(name="王五", age=33))


print(fasm.is_exists(engine,User,{"name":"张三"}))
print(fasm.is_exists(engine,User,{"name":"张三 33"}))

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

相关文章:

  • Oracle ADB 导入 BANK_GRAPH 的学习数据
  • 【C语言】值传递和地址传递
  • 【算法一周目】双指针(2)
  • Vector 深度复制记录
  • INQUIRE:一个包含五百万张自然世界图像,涵盖10,000个不同物种的专为专家级文本到图像检索任务设计的新型基准数据集。
  • 智能电视/盒子的应用管理——通过ADB工具优化体验
  • AI时代保持并提升自身的核心竞争力
  • 常⻅中间件漏洞(WebLogic)靶场
  • 树莓派pico上手
  • 构建高可用和高防御力的云服务架构第五部分:PolarDB(5/5)
  • 今日leetCode 反转字符串
  • 第二证券:股价为什么出现大跌?股价大跌时怎么办?
  • 网安开发:杭州某科技银行面经和答案
  • 轻松获取京东商品信息:商品详情API接口使用教程
  • 关系数据库和非关系数据库
  • 信息安全数学基础(15)欧拉定理
  • 8--SpringBoot原理分析、注解-详解(面试高频提问点)
  • 【Python机器学习】NLP信息提取——值得提取的信息
  • Fyne ( go跨平台GUI )中文文档- 扩展Fyne (七)
  • 是什么推动了今年CSP-J/S报名人数的再创新高?
  • 【AI创作组】Matlab绘图基础之plot函数
  • [Redis][String][上]详细讲解
  • 使用Flink命令行和Java API远程提交Flink任务到Yarn
  • DOM XMLHttpRequest
  • yolov5/8/9/10模型在VOC数据集上的应用【代码+数据集+python环境+GUI系统】
  • 如何切换npm到淘宝的最新镜像源?