Unable to establish connection to sql-server using pyodbc on Windows 10 - python

import pyodbc as sql
import pandas as pd
source_db_config = {
'Trusted_Connection': 'yes',
'driver': '{SQL Server}',
'server': '.',
'database': 'AdventureWorksDW2017',
'autocommit': True,
}
source_connect = sql.connect(**source_db_config)
cannot connect with sql server. It's showing OperationalError:
OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]ConnectionOpen (Connect()). (2)')
How can I solve this?

Python required additional driver to be install to get connect with Databases.
In your case download python driver for sql server
https://learn.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server

Related

How do I fix connection error 08001 Python to SQL Server

I've been trying to connect python with my SQL Server but I get an error and am stuck now.
I work on a mac but for this one on a virtual machine with windows 10 installed.
my code:
import pyodbc
pyodbc.drivers()
['SQL Server',
'SQL Server Native Client 11.0',
'SQL Server Native Client RDA 11.0',
'ODBC Driver 17 for SQL Server']
conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
"Server=HEPHAISTEONAE5E/FUHRP;"
"Database=Fuhrp;"
"Trusted_Connected=yes;"
)
my error is:
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
<ipython-input-18-b79b6302679d> in <module>
----> 1 conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
2 "Server=HEPHAISTEONAE5E/FUHRP;"
3 "Database=Fuhrp;"
4 "Trusted_Connected=yes;"
5 )
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [67]. (67) (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. (67)')
My SQL Server allows remote connections, already checked that.
Hope someone can help me out here.
Thanks! :)

Unable to connect to Microsoft MySQL server using python code getting error

I'm unable to connect to Microsoft MySQL server using Python code. I'm getting error like below
code
import pyodbc
server = 'xxx'
database = 'xxx'
username = 'xxx'
password = 'xx'
driver='{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=portnumber;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
Error
Traceback (most recent call last):
File "sqlCode.py", line 12, in <module>
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=2078;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (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. (2)')
Share my experience,
Maybe it's about SSMS permission problem.
You can go to IP>Security>Login>Account>property to confirm whether the account has the permission to connect to the database.
I have the same code as yours, until I do permission confirmation, it works to me.

pyodbc failed to connect to MSSQL

I want to connect to mssql server using pyodbc but somehow I can't connect.
here is the code
import pyodbc
server = '172.xx.xx.xx,1433'
database = 'DATABASE_BI'
username = 'COMPANY\test.xx'
password = 'test123!##'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
and here is the output
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The
wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL
Server]Login timeout expired (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. (258)')
I don't know what makes it error. Is it because of the mssql server reject the request?

connecting python to sql server using pyodbc

How to connect python to SQL Server using pyodbc until now I wrote the below script but the system crash and display error:
I tried to connect the script to the localhost sql server and i had a successful connection but when I tried on the server on the same network it did crash
code:
import pyodbc
import pandas as pd
conn = pyodbc.connect(
ENGINE='sql_server.pyodbc',
driver='SQL Server',
NAME='testDB',
SERVER ='WIN-CMUH9TBNL53',
DSN='pythonDSN',
PORT='1433',
UID='test',
PWD="test",
)
cursor = conn.cursor()
sql_query=pd.read_sql_query('select * from testDB.dbo.t1',conn)
print(sql_query)
print(type(sql_query))
error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

pyodbc.OperationalError: SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] ODBC SQL Server Driver][

I have searched the web all day tried many solutions but none works. I can manually connect to this server but not with Python I get this error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL
Server Driver][DBNETLIB]SQL Server does not exist or access denied.
(17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server
Driver][DBNETLIB]ConnectionOpen (Connect()). (5); [08001]
[Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
(0)')
I have tried the following:
A)
cnx = pyodbc.connect(Driver='{SQL Server}',
SERVER=self.DBserver,
DATABASE=xyz,
username=self.DBusername,
password=self.DBpassword,
PORT = 1433)
B)
cnx = pyodbc.connect('''Driver={SQL Server};
SERVER=tcp:<self.DBserver>;
PORT=self.DBPort; DATABASE=xyz;
UID=self.DBusername;
PWD=self.DBpassword''')
C)
cnx = = pyodbc.connect(Driver='{SQL Server}',
SERVER=self.DBserver,
DATABASE=xyz,
UID=self.DBusername,
PWD=self.DBpassword)
The issue was with firewall. it is working now
I'm guessing you need something more like this:
cnx = pyodbc.connect('DRIVER={SQL Server};SERVER='+self.DBserver+';DATABASE=xyz;UID='+self.DBusername+';PWD='+self.DBpassword)
I get the same error when trying to connect to Azure SQL Server with pyodbc (4.0.30) when connected via VPN and running in Windows Subsystem for Linux:
with pyodbc.connect(db_connect_string) as conn:
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x274C (10060) (SQLDriverConnect)')
The Python code is running in WSL (5.10.16.3-microsoft-standard-WSL2) / Ubuntu 20.04.3, openssl version shows: OpenSSL 1.1.1k 25 Mar 2021
I could successfully connect with the same credentials via:
MS Data Studio with VPN
same Python/pyodbc code in Powershell with VPN
same Python/pyodbc code in WSL without VPN (plus adding a firewall rule)
My guess is there must be some interaction between OpenSSL and our] VPN, possibly related to these issues.
https://github.com/mkleehammer/pyodbc/issues/853
https://github.com/microsoft/msphpsql/issues/1112

Categories