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

创建模型

python 复制代码
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

创建表

python 复制代码
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)

初始化表

python 复制代码
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)

添加数据

python 复制代码
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)

查询所有数据

python 复制代码
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)

根据字典查询数据

python 复制代码
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 查询

python 复制代码
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 查询

python 复制代码
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 查询

python 复制代码
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)

更新数据

python 复制代码
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 更新数据

python 复制代码
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)

删除数据

python 复制代码
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 删除数据

python 复制代码
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)

根据分页查询数据

python 复制代码
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)

获取第一条数据

python 复制代码
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)

判断数据是否存在

python 复制代码
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"}))
相关推荐
drebander3 分钟前
MySQL 查询优化案例分享
数据库·mysql
西猫雷婶18 分钟前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
初晴~19 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
盖世英雄酱5813624 分钟前
InnoDB 的页分裂和页合并
数据库·后端
老刘莱国瑞1 小时前
STM32 与 AS608 指纹模块的调试与应用
python·物联网·阿里云
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
一只敲代码的猪2 小时前
Llama 3 模型系列解析(一)
大数据·python·llama
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
YashanDB2 小时前
【YashanDB知识库】XMLAGG方法的兼容
数据库·yashandb·崖山数据库