赞
踩
from typing import Optional from sqlmodel import Field, SQLModel, create_engine class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 删除所有表 SQLModel.metadata.drop_all(engine) # 创建所有表 SQLModel.metadata.create_all(engine)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建数据 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.commit()
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.create_all(engine) # 创建数据 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) with Session(engine) as session: statement = select(Hero).where(Hero.name == "Spider-Boy") hero = session.exec(statement).first() print(hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.create_all(engine) with Session(engine) as session: statement = select(Hero) results = session.exec(statement) print(list(results))
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.create_all(engine) with Session(engine) as session: statement = select(Hero) results = session.exec(statement).all() print(results)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # and 查询 with Session(engine) as session: statement = select(Hero).where(Hero.age >= 35, Hero.age < 40) results = session.exec(statement) for hero in results: print(hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = None # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # or 查询 with Session(engine) as session: statement = select(Hero).where(or_(Hero.age <= 35, Hero.age > 90)) results = session.exec(statement) for hero in results: print(hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class Hero(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(index=True) secret_name: str age: int | None = Field(default=None, index=True) # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # or 查询 with Session(engine) as session: statement = select(Hero).where(Hero.age <= 35) results = session.exec(statement) for hero in results: print(hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class Hero(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(index=True) secret_name: str age: int | None = Field(default=None, index=True) # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # 查询 with Session(engine) as session: statement = select(Hero).where(Hero.id == 1) results = session.exec(statement) hero = results.first() print("Hero:", hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class Hero(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(index=True) secret_name: str age: int | None = Field(default=None, index=True) # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # 查询 with Session(engine) as session: hero = session.get(Hero, 1) print("Hero:", hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class Hero(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(index=True) secret_name: str age: int | None = Field(default=None, index=True) # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # 查询 with Session(engine) as session: statement = select(Hero).offset(3).limit(3) results = session.exec(statement) heroes = results.all() print(heroes)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class Hero(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(index=True) secret_name: str age: int | None = Field(default=None, index=True) # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32) hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35) hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36) hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93) with Session(engine) as session: session.add(hero_1) session.add(hero_2) session.add(hero_3) session.add(hero_4) session.add(hero_5) session.add(hero_6) session.add(hero_7) session.commit() # 查询 with Session(engine) as session: statement = select(Hero).where(Hero.name == "Spider-Boy") results = session.exec(statement) hero = results.one() print("Hero:", hero) hero.age = 16 session.add(hero) session.commit() session.refresh(hero) print("Updated hero:", hero)
from typing import Optional from sqlmodel import Field, SQLModel, create_engine, Session, select, or_ class User(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(index=True) age: int | None = Field(default=None, index=True) # 创建数据库引擎 sqlite_url = "mysql+pymysql://root:root@127.0.0.1:3306/fastzdp_sqlmodel?charset=utf8mb4" engine = create_engine(sqlite_url, echo=True) # 创建所有表 SQLModel.metadata.drop_all(engine) SQLModel.metadata.create_all(engine) # 新增 u1 = User(name="张三1", age=23) u2 = User(name="张三2", age=23) u3 = User(name="张三3", age=23) with Session(engine) as session: session.add(u1) session.add(u2) session.add(u3) session.commit() # 查询 with Session(engine) as session: statement = select(User).where(User.name == "张三1") results = session.exec(statement) hero = results.one() print("User: ", hero) session.delete(hero) session.commit() print("Deleted hero:", hero) statement = select(User).where(User.name == "张三1") results = session.exec(statement) hero = results.first() if hero is None: print("不存在名字叫张三1的用户")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。