Why this error when getting data from MSSQL using pyodbc? - python

I am using pyodbc to retrieve data from MSSQL and this is the code I am using:
import pyodbc
server = 'xxxxxxxx\DEV'
database = 'SandBox'
username = 'zzzzzzz'
password = 'xxxxxxx'
driver = '{SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';PORT=4853;SERVER='+server+';PORT=4853;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select * from fieldscreenscheme ")
row = cursor.fetchone()
if row:
print row
This is the error massage I got:
cnxn = pyodbc.connect('DRIVER='+driver+';PORT=43853;SERVER='+server+';PORT=43853;DATABASE='+database+';UID='+username+';PWD='+ password)
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
I installed the ODBC driver. Any recommendation how to solve this error?
I looked at these two but did not help me to solve this.
Python - Can't connect to MS SQL
pyodbc + MySQL + Windows: Data source name not found and no default driver specified
Microsoft Documentation: https://github.com/Microsoft/azure-docs/blob/master/articles/sql-database/sql-database-develop-python-simple.md

Two problems:
Normally, one supplies either the \INSTANCENAME or the port number, not both.
ODBC connection strings for SQL Server do not use PORT=, they put the port number in the SERVER= parameter, e.g., SERVER=xxxxxxxx,43853. (Note that the instance name is omitted and the separator is a comma, not a colon.)

I was facing the same issue whole day wasted and I tried all possible ODBC Driver for SQL Server here is the list, I just try one by one and it works for me
Driver={ODBC Driver 11 for SQL Server} for SQL Server 2005 - 2014
Driver={ODBC Driver 13 for SQL Server} for SQL Server 2005 - 2016
Driver={ODBC Driver 13.1 for SQL Server} for SQL Server 2008 - 2016
Driver={ODBC Driver 17 for SQL Server} for SQL Server 2008 - 2017
And these are some others we can use, somehow in my case the last one works :)
Driver={SQL Server} for SQL Server 2000
Driver={SQL Native Client} for SQL Server 2005
Driver={SQL Server Native Client 10.0} for SQL Server 2008
Driver={SQL Server Native Client 11.0} for SQL Server 2012

I've met same problem and fixed it changing connection string like below.
Writing
driver = '{ODBC Driver 13 for SQL Server}'
instead of
driver = '{SQL Server}'

Related

Can't connect to Azure SQL DB - pyodbc Operational Error

Attempting to connect to an Azure SQL database, most other configurations I've tried result in an instant error:
Client unable to establish connection (0) (SQLDriverConnect)')
But this one I'm currently trying times out and is the closest I've got:
cnxn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', server='tcp:mydatabase.database.windows.net:1433', database='MyDatabase', user='username', password='password')
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
There is no requirement for the connection string to include the default SQL Server port, which is 1433.
Add server name without tcp, use tcp: before quotes of servername, Server name format is servername.database.windows.net
Example String
pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
The port defined on the server attribute is separated by comma and not colon.
Driver={ODBC Driver 1x for SQL Server};Server=[tcp]:{serverName}.database.windows.net[,1433]; (...)
You can check a sample of the connection string on the connections string blade on your database page on Azure Portal.

pyodbc.drivers() returns the list of drivers but my python script isn't running with any ODBC driver listed

Ran print(pyodbc.drivers()) and it printed the list of available drivers... running a SQL container in docker I'm trying to manipulate the database from a python script
connector = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};' 'Server=cbbcb967dacb;' 'Database=testdb;' 'UID=sa;' 'PWD=Working#2022' 'Trusted_Connection=yes;')
connector.execute()
cursor = connector.cursor()
cursor.execute('SELECT * FROM inventory')
for i in cursor:
print(i)
The error I get is:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 18 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)')
But the ODBC Driver is installed
The exception suggests a network error, cbbcb967dacb looks like the hostname of the container, you might want to try its IP address directly or localhost if you ran SQL Server in docker as per the documentation.
Are you able to telnet cbbcb967dacb 1433 ?
Based on the discussion in the comments, if your docker container is running and was started as per the MSSQL tutorial, the following connection string should work:
"DRIVER={ODBC Driver 18 for SQL Server};SERVER=localhost;UID=SA;PWD={Working#2022};DATABASE=testdb;"

pyodbc connection failing in sqlalchemy but working through direct pyodbc connection

Why does this workc(I get a result set back):
sql_server = 'myserver.database.windows.net'
sql_database = 'pv'
sql_username = 'sqladmin'
sql_password = 'password1'
sql_driver= '{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+sql_driver+';SERVER=tcp:'+sql_server+';DATABASE='+sql_database+';UID='+sql_username+';PWD='+ sql_password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 3 SAPPHIRE_CASE_ID FROM PV_ALL_SUBMISSIONS_SL")
row = cursor.fetchone()
while row:
print (str(row[0]))
row = cursor.fetchone()
But this fails:
import pyodbc
sql_engine = sqlalchemy.create_engine(f'mssql+pyodbc://{sql_username}:{sql_password}#{sql_server}/{sql_database}?driver=ODBC+Driver+17+for+SQL+Server')
df.to_sql('PV_ALL_CLOSED_CASES_SL', con=sql_engine, if_exists='append')
Error is:
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]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/14/e3q8)
While I know one is doing a read and the other a write, my issue seems to be just establishing a connection one way vs another, when using the same connection details. It isn't an Azure firewall issue as I am able to connect and run a select statment via the first method, but when using create_engine() of sqlalchemy, it fails to make the connection - but I am pretty sure the connection string is correct.
It is the same variables for server, user name and password being used in both connections.
I think the issue is that the real password as an "#" symbol in it, and so this interferes with the latter connection string.
Thanks to #Larnu, this worked:
from sqlalchemy.engine import URL
connection_string = f"DRIVER={sql_driver};SERVER={sql_server};DATABASE={sql_database};UID={sql_username};PWD={sql_password}"
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
sql_engine = sqlalchemy.create_engine(connection_url)
I dont have to url encode when I use a cx_Oracle connection, but hey it works now.

SQL Alchemy with SQL Server

I wasn't sure what to title my post, if you have a better idea, feel free to edit the title.
I have not used SQL Alchemy before and the documentation that I have looked at located in the following places, is not helpful:
Connecting to SQL Database Using SQL Alchemy in Python
Tutorial Point
Here is the code I am using:
import sqlalchemy as sal
from sqlalchemy import create_engine
#Here are the parameters I am using:
- server = 'Q-20/fake_example'
- database = 'AdventureWorks2017'
- driver = 'ODBC Driver 17 for SQL Server'
- trusted_connection='yes'
DATABASE_CONNECTION = 'mssql+pyodbc://#server = ' + server + '/database = ' + database + '?trusted_connection = ' + trusted_connection + '&driver=' + driver
engine = sal.create_engine(DATABASE_CONNECTION)
All of that seems to work fine without any problems; however, when I add this line:
connection=engine.connect()
I get the following error message:
sqlalchemy.exc.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)')
I am not sure what is wrong with what I am doing, does anyone have any suggestions?
What I have tried so far:
I have confirmed that SQL Server is configured to allow remote connections. I did this check by following the instructions here
Removing the "#" sign before the server, but this just generated the same error message.
I figured out part of what I needed to do. I needed to change my parameters.
Old Parameters:
server = 'Q-20/fake_example'
database = 'AdventureWorks2017'
driver = 'ODBC Driver 17 for SQL Server'
trusted_connection='yes'
New Parameters:
server = 'Q-20'
database = 'AdventureWorks2017'
driver = 'SQL+SERVER+NATIVE+CLIENT+11.0'
trusted_connection='yes'
This is what my code ultimately looked like:
database_connection = 'mssql+pyodbc://Q-20/AdventureWorks2017?trusted_connection=yes&driver=SQL+SERVER+NATIVE+CLIENT+11.0'

Connecting to MS SQL Server on a remote desktop from Python

I am connecting to a SQL Server hosted on a remote desktop using Windows server through VBA with this code:
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa;Password=xxxxx;Persist Security Info=True;Initial Catalog=databaseName;Data Source=192.168.1.xxx;"
objMyConn.Open
Currently trying to use python to connect to the same SQL Server database with this code:
import pyodbc
server_name='192.168.1.xxx'
db_name='databaseName'
username='sa'
password='xxxxx'
conn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};'
'Server=server_name;'
'Database=db_name;'
'UID=username;'
'PWD=password;'
'Trusted_Connection=yes;')
cursor=conn.cursor()
TRACEBACK:
File "x/test.py", line 6, in <module>
conn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};'
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 11 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)')
You should omit argument trusted_connection since you are providing UID and PWD. Trusted_connection fills UID and PWD values with your current windows user values so it is probably reserved for a local connection within the host.
I belive connection string should look like this:
'DRIVER={ODBC Driver 11 for SQL Server};'
'Server=server_name;'
'Database=db_name;'
'UID=username;'
'PWD=password;'

Categories

Resources