mysql 异步api_FastApi教程|异步SQL(关系)数据库

您也可以将

FastAPI

encode/databases

FastAPI

一起

使用

以使用

async

连接到数据库

await

它兼容:

PostgreSQL的

的MySQL

SQLite的

在此示例中,我们将使用

SQLite

,因为它使用单个文件并且Python具有集成的支持。

因此,您可以复制此示例并按原样运行它。

稍后,对于您的生产应用程序,您可能想要使用

PostgreSQL之

类的数据库服务器

小费

您可以采用有关SQLAlchemy ORM(

SQL(关系)数据库

)

部分的想法

,例如使用实用程序函数独立于

FastAPI

代码

在数据库中执行操作

本部分不应用这些想法,等同于

Starlette中

的对应

思想

导入和设置

SQLAlchemy

导入

SQLAlchemy

创建一个

metadata

对象。

notes

使用

metadata

对象

创建表

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

小费

请注意,所有这些代码都是纯SQLAlchemy Core。

databases

在这里还没有做任何事情。

导入和设置

databases

导入

databases

创建一个

DATABASE_URL

创建一个

database

对象。

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

小费

如果要连接到其他数据库(例如PostgreSQL),则需要更改

DATABASE_URL

创建表

在这种情况下,我们将在同一Python文件中创建表,但是在生产中,您可能希望使用Alembic创建它们,并与迁移集成等。

在这里,此部分将在启动

FastAPI

应用程序

之前直接运行

创建一个

engine

metadata

对象

创建所有表

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

创建模型

创建Pydantic模型用于:

要创建的注释(

NoteIn

)。

要返回的注释(

Note

)。

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

通过创建这些Pydantic模型,将对输入数据进行验证,序列化(转换)和注释(记录)。

因此,您将能够在交互式API文档中看到全部内容。

连接和断开连接

创建您的

FastAPI

应用程序。

创建事件处理程序以连接数据库并与数据库断开连接。

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

阅读笔记

创建

路径操作函数

以读取注释:

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

注意

请注意,当我们使用与数据库进行通信时

await

路径操作函数

是用声明的

async

注意

response_model=List[Note]

它使用

typing.List

的文档(和验证,串行化,过滤器)的输出数据,作为一个

list

Note

创建笔记

创建

路径操作函数

以创建注释:

from typing import List

import databases

import sqlalchemy

from fastapi import FastAPI

from pydantic import BaseModel

# SQLAlchemy specific code, as with any other app

DATABASE_URL = "sqlite:///./test.db"

# DATABASE_URL = "postgresql://user:password@postgresserver/db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(

"notes",

metadata,

sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),

sqlalchemy.Column("text", sqlalchemy.String),

sqlalchemy.Column("completed", sqlalchemy.Boolean),

)

engine = sqlalchemy.create_engine(

DATABASE_URL, connect_args={"check_same_thread": False}

)

metadata.create_all(engine)

class NoteIn(BaseModel):

text: str

completed: bool

class Note(BaseModel):

id: int

text: str

completed: bool

app = FastAPI()

@app.on_event("startup")

async def startup():

await database.connect()

@app.on_event("shutdown")

async def shutdown():

await database.disconnect()

@app.get("/notes/", response_model=List[Note])

async def read_notes():

query = notes.select()

return await database.fetch_all(query)

@app.post("/notes/", response_model=Note)

async def create_note(note: NoteIn):

query = notes.insert().values(text=note.text, completed=note.completed)

last_record_id = await database.execute(query)

return {**note.dict(), "id": last_record_id}

注意

请注意,当我们使用与数据库进行通信时

await

路径操作函数

是用声明的

async

关于

{**note.dict(), "id": last_record_id}

note

是一个Pydantic

Note

对象。

note.dict()

返回

dict

带有其数据的,例如:

{

"text": "Some note",

"completed": False,

}

但它没有该

id

领域。

因此,我们创建了一个new

dict

,其中包含来自的键值对

note.dict()

{**note.dict()}

**note.dict()

直接将键值对“解包”,因此

{**note.dict()}

或多或少是的副本

note.dict()

然后,我们扩展该副本

dict

,添加另一个键值对

"id": last_record_id

{**note.dict(), "id": last_record_id}

因此,返回的最终结果将类似于:

{

"id": 1,

"text": "Some note",

"completed": False,

}

检查它

您可以按原样复制此代码,并参阅

http://127.0.0.1:8000/docs上

的文档

在那里,您可以看到所有API记录在案并与之交互:

e72a7d7301f67ac01cd069af2d4903a6.png

更多信息

您可以

encode/databases

在其GitHub页面上

了解更多信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值