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"}))
相关推荐
HoneyMoose几秒前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热2 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
LZXCyrus3 分钟前
【杂记】vLLM如何指定GPU单卡/多卡离线推理
人工智能·经验分享·python·深度学习·语言模型·llm·vllm
是老余3 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar
crazy_wsp4 分钟前
IDEA怎么定位java类所用maven依赖版本及引用位置
java·maven·intellij-idea
.Ayang6 分钟前
tomcat 后台部署 war 包 getshell
java·计算机网络·安全·web安全·网络安全·tomcat·网络攻击模型
Enougme6 分钟前
Appium常用的使用方法(一)
python·appium
一直学习永不止步12 分钟前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表
懷淰メ12 分钟前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
hummhumm26 分钟前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j