0

So, I am trying to connect to SQL SERVER using this: conn = pyodbc.connect(Driver="SQL Server", Server="DESKTOP-UDM7FMJ", Database="TestDB", uid="sa", pwd="123456")

That actually works just fine enter image description here

But if I try to build the string like this

conn_info = r'Driver="SQL Server", Server="DESKTOP-UDM7FMJ", Database="TestDB", uid="sa", pwd="123456"'
  print (conn_info)
    
  conn = pyodbc.connect(conn_info)

Then I have issues. The print gives me the same string used previously. enter image description here

However, I get the following error

  Message=('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
  Source=I:\Windows10\DatabaseHealth\PythonApplication2\Main.py
  StackTrace:
  File "I:\Windows10\DatabaseHealth\PythonApplication2\Main.py", line 16, in <module>
    conn = pyodbc.connect(conn_info)

Why is that error showing up?

mrivanlima
  • 561
  • 4
  • 10
  • Your first example uses *keyword arguments* to the `.connect()` method, e.g., `Driver="SQL Server"`. Your second example includes double-quotes *within* the connection string, and that's wrong. It should be `r'Driver=SQL Server;…'` – Gord Thompson Dec 30 '20 at 17:58
  • I tried that, I still get the same error. – mrivanlima Dec 30 '20 at 18:02
  • Have you tried passing the connection info using a dictionary rather than a string? – Smurphy0000 Dec 30 '20 at 18:08
  • Yes, I did. In fact, I came to only pass the string after having issues with a dictionary. This is what I had for dict: conn_info = 'Driver="SQL Server", Database ="% s", Server="% s", uid=% s; pwd="% s"'% (params [0], params [1], params [2], params [3 ]) – mrivanlima Dec 30 '20 at 18:10
  • You're still putting double-quotes around the driver name and using commas instead of semi-colons. `'Driver="SQL Server", …'"` simply will not work. – Gord Thompson Dec 30 '20 at 18:14
  • Gord Thompson, that is not the problem. I tried with and without it. – mrivanlima Dec 30 '20 at 18:19
  • 1
    `conn_info = 'Driver=SQL Server;Server=DESKTOP-UDM7FMJ;Database=TestDB;uid=sa;pwd=123456'` – Gord Thompson Dec 30 '20 at 18:22
  • Alright, Gordon, I see what you did now. Spaces between equal signs had to be removed as well. It works now. Thanks! – mrivanlima Dec 30 '20 at 18:28

0 Answers0