connecting python to sql server using pyodbc - python

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)')

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?

SQL Alchemy create_engine connection string won't work

I'm trying to write to MSSQL using SQL Alchemy but can't get the connection to work. I've got
engine = create_engine('mssql+pyodbc://<Username>#localhost:5432/<DBName>?driver=SQL+Server+Native+Client+11.0')
df.to_sql('<TableName>', con=engine, if_exists='append')
This gives:
OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]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. (10061)')
(Background on this error at: http://sqlalche.me/e/13/e3q8)
I've looked at the error documentation and tried several different things with the connection string but nothing seems to work. Reading from the same database using pyodbc works fine:
con = pyodbc.connect('Driver={SQL Server};'
'Server=<Server>;'
'Database=<DBName>;'
'Trusted_Connection=yes;')
df = pd.read_sql_query('select * from <DBName>.dbo.<TableName>', con)

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