3

I need to connect my langchain database agent with my sqlserver database so that my agent can access the data, yet in the documention it does not explain how to do so, it only shows how to connect to sqlite like the following code:

from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI


db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

I have tried to replace this code:

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

with this:

db = SQLDatabase.from_uri("mssql+pyodbc://{server}/{database}?driver{driver}")

but the following error showed up:

sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: https://sqlalche.me/e/20/rvf5)**

5
  • Have you contacted the vendor to see if they even support SQL Server? That might be the reason you don't see an example in the documentation. Commented Aug 26, 2023 at 18:00
  • @AaronBertrand Indeed they do Commented Aug 26, 2023 at 18:06
  • 1
    Ok, so did you ask their support team how to do it? I've never heard of this product before and I doubt many others here have either. So I would still start with the vendor. Commented Aug 26, 2023 at 18:09
  • 1
    Some basic guidance is here [docs.sqlalchemy.org/en/20/core/… (`SQLDatabase is a wrapper around a SQLAlchemy engine, as the error message suggests). Commented Aug 26, 2023 at 18:49
  • Have you tried driver={driver}? And what's stored in the driver variable? e.g.: if you have ODBC Driver 17 for SQL Server installed then it should end up with driver=ODBC Driver 17 for SQL Server encoded in the URI as driver=ODBC+Driver+17+for+SQL+Server Ref: SQLAlchemy # Microsoft SQL Server Commented Aug 27, 2023 at 1:18

2 Answers 2

0

I've tested this and it seems to be working.

uri = 'mssql+pyodbc://DBUsername:DBPassword@ServerName:Port/DBName?driver=ODBC+Driver+17+for+SQL+Server'
db = SQLDatabase.from_uri(uri)

Ref : https://github.com/langchain-ai/langchain/issues/9804

Sign up to request clarification or add additional context in comments.

2 Comments

the link of the github issue which you provided is made by me lol, I am programmersEmperor. Thanks anyway
I followed this but still getting error i.e. DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)") (Background on this error at: sqlalche.me/e/14/dbapi)
0

try this

db=SQLDatabase.from_uri("mssql+pyodbc:///?odbc_connect=DRIVER={ODBC Driver 18 for SQL Server};SERVER=10.20.10.10;DATABASE=dbanem;UID=username;PWD=password;Encrypt=no",sample_rows_in_table_info=3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.