创建模型
from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasmclass Hero(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strsecret_name: strage: Optional[int] = None
创建表
from typing import Optional
from sqlmodel import Field, SQLModel
import fastzdp_sqlmodel as fasmclass Hero(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strsecret_name: strage: Optional[int] = Noneengine = 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 fasmclass Hero(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strsecret_name: strage: Optional[int] = Noneengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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 fasmclass User(SQLModel, table=True):id: Optional[int] = Field(default=None, primary_key=True)name: strage: intengine = 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"}))