Langchain 无法连接 Azure SQL 数据库

问题描述 投票:0回答:1

尝试连接我的 Azure SQL 数据库,它包含多个表。

这是我的代码示例

from sqlalchemy import create_engine

driver = '{ODBC Driver 17 for SQL Server}'
odbc_str = 'mssql+pyodbc:///?odbc_connect=' \
                'Driver='+driver+ \
                ';Server=tcp:' + os.getenv("SQL_SERVER")+'.database.windows.net;PORT=1433' + \
                ';DATABASE=' + os.getenv("SQL_DB") + \
                ';Uid=' + os.getenv("SQL_USERNAME")+ \
                ';Pwd=' + os.getenv("SQL_PWD") + \
                ';Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'

db_engine = create_engine(odbc_str)
from langchain.agents import AgentType, create_sql_agent
from langchain.sql_database import SQLDatabase
from langchain.agents.agent_toolkits.sql.toolkit import SQLDatabaseToolkit

db = SQLDatabase(db_engine)

这是我抛出的错误:

Error: ('HY104', '[HY104] [Microsoft][ODBC SQL Server Driver]Invalid precision value (0) (SQLBindParameter)')

The above exception was the direct cause of the following exception:

DBAPIError                                Traceback (most recent call last)
Cell In[16], line 5
      2 from langchain.sql_database import SQLDatabase
      3 from langchain.agents.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
----> 5 db = SQLDatabase(db_engine)
      7 sql_toolkit = SQLDatabaseToolkit(db=db, llm=llm)
      8 sql_toolkit.get_tools()
...
[SQL: SELECT [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME] 
FROM [INFORMATION_SCHEMA].[TABLES] 
WHERE [INFORMATION_SCHEMA].[TABLES].[TABLE_SCHEMA] = CAST(? AS NVARCHAR(max)) AND [INFORMATION_SCHEMA].[TABLES].[TABLE_TYPE] = CAST(? AS NVARCHAR(max)) ORDER BY [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME]]
[parameters: ('dbo', 'BASE TABLE')]
(Background on this error at: https://sqlalche.me/e/20/dbapi)
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

有人有想法吗?

我想连接到我的 Azure SQL 数据库,但它不起作用并抛出错误。我尝试更改驱动程序和加密等,但这一切似乎都不起作用。

azure azure-sql-database langchain
1个回答
0
投票
Error: ('HY104', '[HY104] [Microsoft][ODBC SQL Server Driver]Invalid precision value (0) (SQLBindParameter)')

根据this,该问题可能会得到较低版本的SQL alchemy包,请使用兼容版本的SQL alchemy。您可以使用以下代码将Azure SQL数据库连接到Lang链:

import urllib
import sqlalchemy
from langchain.agents import AgentType, create_sql_agent
from langchain.sql_database import SQLDatabase
from langchain.agents.agent_toolkits.sql.toolkit import SQLDatabaseToolkit

server = '<serverName>.database.windows.net'
database = '<dbName>'
username = '<userName>'
password = '<password>'
driver = '{ODBC Driver 17 for SQL Server}'
odbc_str = 'DRIVER='+driver+';SERVER='+server+';PORT=1433;UID='+username+';DATABASE='+ database + ';PWD='+ password
connect_str = 'mssql+pyodbc:///?odbc_connect=' + urllib.parse.quote_plus(odbc_str)
engine = sqlalchemy.create_engine(connect_str)
db = SQLDatabase(engine)
db.run("SELECT [TABLE_NAME] FROM [INFORMATION_SCHEMA].[TABLES] WHERE [TABLE_SCHEMA] = 'dbo' AND [TABLE_TYPE] = 'BASE TABLE' ORDER BY [TABLE_NAME]")   

您将得到如下所示的输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.