Python not connecting with MS SQL Server

Hi All,

I have Power BI and Python install in my local machine , and the MS SQL server in install in Remote Desktop.

I install pypyodbc and pyodbc in local machine and trying to connect my db and tables by unable to do that.

mport pandas as pd
import numpy as np
import os

import datetime 

import pyodbc as odbc

DRIVER_NAME ='SQL SERVER'
SERVER_NAME= 'servername'
DATABASE_NAME = 'dbname'

connection_string =f"""
    DRIVER={{{DRIVER_NAME}}};
    SERVER={SERVER_NAME};
    DATABASE={DATABASE_NAME};
    Trust_Copnnection=yes;
"""

conn=odbc.connect(connection_string)
print(conn)

getting this error

<pyodbc.Connection object at 0x0000020C9B21FB80>

Do I need to install MS SQL Server in my local machine and also the driver ?

This is not an error. You’ve called odbc.connect(connection_string)
and it returned you an object representing the connection. It didn’t
raise an exception. That is success.

Then you print(conn), which prints the conn object you received. The
output tells you that you have a pyodbc.Connection object, along with
its memory address (broadly useless to you, ignore it).

This is all fine.

Go and use your conn object to work with the database.

3 Likes

Hi Suvechha. It looks like you succeed with something I could use help with.
What did you use for SERVER_NAME?
I tried, I think, every combination:
Public IPv4 DNS\instance_name
Private IPv4 address\instance_name
Public IPv4 address\instance_name
etc…

for <instance_name> I was using the field circled in red:
image

Which is the right combination?