Skip to main content

SQL数据库

note

SQLDatabase适配器工具是数据库连接的封装。

在与SQL数据库交互时,它使用SQLAlchemy核心API。

本笔记本展示了如何使用该工具访问SQLite数据库。 它使用示例Chinook数据库,并演示了以下功能:

  • 使用SQL查询
  • 使用SQLAlchemy可选查询
  • 获取模式cursorallone
  • 绑定查询参数

您可以使用Tool@tool装饰器从该工具创建工具。

::: {.callout-caution} 如果从SQLDatabase工具创建工具并将其与LLM结合使用或暴露给最终用户,请记得遵循良好的安全实践。

有关安全信息,请参见:https://python.langchain.com/docs/security :::

!wget 'https://github.com/lerocha/chinook-database/releases/download/v1.4.2/Chinook_Sqlite.sql'
!sqlite3 -bail -cmd '.read Chinook_Sqlite.sql' -cmd 'SELECT * FROM Artist LIMIT 12;' -cmd '.quit'
1|AC/DC
2|Accept
3|Aerosmith
4|Alanis Morissette
5|Alice In Chains
6|Antônio Carlos Jobim
7|Apocalyptica
8|Audioslave
9|BackBeat
10|Billy Cobham
11|Black Label Society
12|Black Sabbath
!sqlite3 -bail -cmd '.read Chinook_Sqlite.sql' -cmd '.save Chinook.db' -cmd '.quit'

初始化数据库

from pprint import pprint

import sqlalchemy as sa
from langchain_community.utilities import SQLDatabase

db = SQLDatabase.from_uri("sqlite:///Chinook.db")

查询作为游标

获取模式 cursor 返回结果作为 SQLAlchemy 的 CursorResult 实例。

result = db.run("SELECT * FROM Artist LIMIT 12;", fetch="cursor")
print(type(result))
pprint(list(result.mappings()))
<class 'sqlalchemy.engine.cursor.CursorResult'>
[{'ArtistId': 1, 'Name': 'AC/DC'},
{'ArtistId': 2, 'Name': 'Accept'},
{'ArtistId': 3, 'Name': 'Aerosmith'},
{'ArtistId': 4, 'Name': 'Alanis Morissette'},
{'ArtistId': 5, 'Name': 'Alice In Chains'},
{'ArtistId': 6, 'Name': 'Antônio Carlos Jobim'},
{'ArtistId': 7, 'Name': 'Apocalyptica'},
{'ArtistId': 8, 'Name': 'Audioslave'},
{'ArtistId': 9, 'Name': 'BackBeat'},
{'ArtistId': 10, 'Name': 'Billy Cobham'},
{'ArtistId': 11, 'Name': 'Black Label Society'},
{'ArtistId': 12, 'Name': 'Black Sabbath'}]

查询作为字符串负载

获取模式 allone 返回字符串格式的结果。

result = db.run("SELECT * FROM Artist LIMIT 12;", fetch="all")
print(type(result))
print(result)
<class 'str'>
[(1, 'AC/DC'), (2, 'Accept'), (3, 'Aerosmith'), (4, 'Alanis Morissette'), (5, 'Alice In Chains'), (6, 'Antônio Carlos Jobim'), (7, 'Apocalyptica'), (8, 'Audioslave'), (9, 'BackBeat'), (10, 'Billy Cobham'), (11, 'Black Label Society'), (12, 'Black Sabbath')]
result = db.run("SELECT * FROM Artist LIMIT 12;", fetch="one")
print(type(result))
print(result)
<class 'str'>
[(1, 'AC/DC')]

带参数的查询

为了绑定查询参数,请使用可选的 parameters 参数。

result = db.run(
"SELECT * FROM Artist WHERE Name LIKE :search;",
parameters={"search": "p%"},
fetch="cursor",
)
pprint(list(result.mappings()))
[{'ArtistId': 35, 'Name': 'Pedro Luís & A Parede'},
{'ArtistId': 115, 'Name': 'Page & Plant'},
{'ArtistId': 116, 'Name': 'Passengers'},
{'ArtistId': 117, 'Name': "Paul D'Ianno"},
{'ArtistId': 118, 'Name': 'Pearl Jam'},
{'ArtistId': 119, 'Name': 'Peter Tosh'},
{'ArtistId': 120, 'Name': 'Pink Floyd'},
{'ArtistId': 121, 'Name': 'Planet Hemp'},
{'ArtistId': 186, 'Name': 'Pedro Luís E A Parede'},
{'ArtistId': 256, 'Name': 'Philharmonia Orchestra & Sir Neville Marriner'},
{'ArtistId': 275, 'Name': 'Philip Glass Ensemble'}]

使用 SQLAlchemy 可选择项的查询

除了纯文本 SQL 语句,适配器还接受 SQLAlchemy 可选择项。

# In order to build a selectable on SA's Core API, you need a table definition.
metadata = sa.MetaData()
artist = sa.Table(
"Artist",
metadata,
sa.Column("ArtistId", sa.INTEGER, primary_key=True),
sa.Column("Name", sa.TEXT),
)

# Build a selectable with the same semantics of the recent query.
query = sa.select(artist).where(artist.c.Name.like("p%"))
result = db.run(query, fetch="cursor")
pprint(list(result.mappings()))
[{'ArtistId': 35, 'Name': 'Pedro Luís & A Parede'},
{'ArtistId': 115, 'Name': 'Page & Plant'},
{'ArtistId': 116, 'Name': 'Passengers'},
{'ArtistId': 117, 'Name': "Paul D'Ianno"},
{'ArtistId': 118, 'Name': 'Pearl Jam'},
{'ArtistId': 119, 'Name': 'Peter Tosh'},
{'ArtistId': 120, 'Name': 'Pink Floyd'},
{'ArtistId': 121, 'Name': 'Planet Hemp'},
{'ArtistId': 186, 'Name': 'Pedro Luís E A Parede'},
{'ArtistId': 256, 'Name': 'Philharmonia Orchestra & Sir Neville Marriner'},
{'ArtistId': 275, 'Name': 'Philip Glass Ensemble'}]

带执行选项的查询

可以通过自定义执行选项来增强语句调用。例如,当应用模式名称转换时,后续语句将失败,因为它们试图访问一个不存在的表。

query = sa.select(artist).where(artist.c.Name.like("p%"))
db.run(query, fetch="cursor", execution_options={"schema_translate_map": {None: "bar"}})

相关


此页面是否有帮助?


您还可以留下详细的反馈 在 GitHub 上