Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python connect to mssql
#1
Hi, I'm new to python
I'm currently converting my .net application to python.

I'm trying to call database to extract some data.
I manage to call to mssql database using pyodbc, but when I tried to use pandas, it failed with error
Error:
Exception has occurred: OperationalError (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)') (Background on this error at: https://sqlalche.me/e/20/e3q8)
Here is my pyodbc code
connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER="+server+";DATABASE="+database+";UID="+username+";PWD="+ password
conn = pyodbc.connect(connection_string)
cursor = conn.cursor()
cursor.execute(f"SELECT COUNT(*) FROM [98_PRODUCTS] WHERE SKU = ?", (sku,))
count = cursor.fetchone()[0]
this work fine

but when I want to try to use pandas
engine = create_engine('mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30')
data = pd.read_sql(query, engine)
it get error mentioned above.

May I know why this happened?? and how to solve this issue?


btw, I'm using Windows 11 running on Macbook M1 Parallel Desktop.
buran write Dec-06-2023, 08:00 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Shouldn't be an f-string?
engine = create_engine(f'mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30')
That is assuming username, password, server and database used are actually names
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for helping to add the tag.
I tried with and without f also same.
I assume f is formatting only and yes, username/password/etc are all string

(Dec-06-2023, 08:09 AM)buran Wrote: Shouldn't be an f-string?
engine = create_engine(f'mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30')
That is assuming username, password, server and database used are actually names
Reply
#4
(Dec-06-2023, 08:26 AM)wailoonho Wrote: I assume f is formatting only
f-strings allows to interpolate the string, i.e. use values assign to names. without it the connection string is literally
mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30'
I would suggest that you construct the string outside of call and print it to check that is correct

I would try
conn_string = f'mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server'
engine = create_engine(conn_string, connect_args={'timeout': 30})

see the example in the docs
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I tried, here is my code

                    strConnString = f'mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30'
                    print(strConnString)
                    engine = create_engine(strConnString)
here is the printed out connection string
Quote:mssql+pyodbc://sa:P@ssw0rd@myserver/LightStickers?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30

And here is the error this time
Error:
Exception has occurred: OperationalError (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)') (Background on this error at: https://sqlalche.me/e/20/e3q8) pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)')
(Dec-06-2023, 08:59 AM)buran Wrote:
(Dec-06-2023, 08:26 AM)wailoonho Wrote: I assume f is formatting only
f-strings allows to interpolate the string, i.e. use values assign to names. without it the connection string is literally
mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&connect_timeout=30'
I would suggest that you construct the string outside of call and print it to check that is correct

I would try
conn_string = f'mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server'
engine = create_engine(conn_string, connect_args={'timeout': 30})

see the example in the docs
Reply
#6
Does your password indeed has @?
if yes, check Writing a connection string when password contains special characters
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
the password is P@ssw0rd

(Dec-06-2023, 12:22 PM)buran Wrote: Does your password indeed has @?
if yes, check Writing a connection string when password contains special characters
Reply
#8
Thanks Buran, it works now. technically just the special character on the password.

(Dec-06-2023, 12:22 PM)buran Wrote: Does your password indeed has @?
if yes, check Writing a connection string when password contains special characters
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  connect sql by python using txt. file dawid294 2 445 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  How to Connect to PostgreSQL Through Jump Server and SSH Tunnel using Python? nishans 1 1,018 Jan-02-2024, 10:37 AM
Last Post: khanzain
  Using Python to connect to an XML ? jehoshua 12 1,994 Jul-11-2023, 12:34 AM
Last Post: jehoshua
  MSSQL query not working in Python kat35601 0 926 Apr-12-2022, 06:44 PM
Last Post: kat35601
  from MSSQL to excel kat35601 1 1,678 Apr-11-2022, 06:19 PM
Last Post: buran
  Trying to make a bot to connect on discord with Selenium Python johnsmith43 2 52,042 Mar-21-2022, 02:56 PM
Last Post: Cloudytechnical
  How to connect Mysql databse with python and VSCode IDE madhusoodhananER 1 8,738 Oct-31-2019, 10:15 AM
Last Post: Larz60+
  Connect a Teradata DB to Python OscarBoots 10 8,888 Jan-31-2019, 10:23 PM
Last Post: OscarBoots
  [split] How to insert JSON into MSSQL gehrenfeld 3 3,315 Jan-27-2019, 06:20 PM
Last Post: micseydel
  How to insert JSON into MSSQL raysefo 2 4,140 Jan-27-2019, 06:17 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020