Mariska
Mariska

Reputation: 1949

SQL connection works in VBA but not in Python

I have a code in VBA that reads in from a SQL server whose parameters are set as follows.

Global Const my_db = "Driver={SQL Server};Server=my_server;Initial Catalog=my_catalog;UID=user;PWD=opensesame;"

However, translating into Python as such didn't work for me:

import _mysql
db=_mysql.connect(host="my_server",user="user",passwd="opensesame",db="my_db")

The error I received is

Traceback (most recent call last):
  File ".\db_test.py", line 6, in <module>
    db=_mysql.connect(host="my_server",user="user",passwd="opensesame",db="my_db")
  File "C:\python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'my_server' (10061)")

What am I doing wrong?

Upvotes: 0

Views: 208

Answers (1)

Kamran
Kamran

Reputation: 3537

your SQL server is MS SQL server. Your VBA script connects to SQL server on port 1433. Python script is trying to connect to MySQL server on port 3306 (default port of MySQL).

Try this:

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=my_server;Initial Catalog=my_catalog;UID=user;PWD=opensesame'

Upvotes: 1

Related Questions