Search code examples
python-3.xmysql-connector-python

Connection to remote SQL Server via Windows Credentials


I'm trying to convert some VBA code to Python 3. The code basically accesses a remote database, using the Windows Credentials of the user. I'm struggling to get the connection working.

My VBA code was:

Function Connect(Server As String, _
                 Database As String) As Boolean

    Set CN = New ADODB.Connection
    On Error Resume Next

    With CN
        ' Create connecting string
        .ConnectionString = "Provider=SQLOLEDB.1;" & _
                            "Integrated Security=SSPI;" & _
                            "Server=" & Server & ";" & _
                            "Database=" & Database & ";"
        ' Open connection
        .Open
    End With

In Python I'm trying:

import mysql.connector
mydb = mysql.connector.connect(
    host="server as above", 
    database = "database as above", 
    IntegratedSecurity = True
)

print(mydb)

I suspect I'm just being dumb, and I've searched, but failing to find any real help on this. Any advice (or a link to where it already been answered) would be much appreciated.


Solution

  • Solved my own question.

    import pyodbc
    
    conn = pyodbc.connect('Driver={SQL Server};'
                          'Server=GDC-CXL-DB-PROD.zug.novatek.int;'
                          'Database=CXLPROD;'
                          'Trusted_Connection=yes;')
    SQL = {string}
    
    data = pd.read_sql(SQL,conn)