pyodbc InterfaceError - python

I'm having trouble connecting to a MS SQL Server database via Python using pyODBC:
import pyodbc
conn = pyodbc.connect(('DRIVER={SQL Server};'
'SERVER=tmw-prod-lo\\SQL65;'
'DATABASE=TMWSuite;'
'UID=VENTURE\\acoop;'
'PWD=RaNdoM!'))
This results in:
InterfaceError: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'VENTURE\\acoop'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'VENTURE\\acoop'. (18456)")
I'm pretty confident I've got all the parameters right and that I need to escape the backslashes in the server and username as above, correct? So it just looks like a permissions issue but I wanted to rule out any other issues first.

Related

Pyodbc - Login Failed for user - Confirmed user information is correct and working

I am connecting to an SQL server. This application was working last week. The server lost power over the weekend and shutdown. I came in this morning and turned everything on, and now I cannot connect to the SQL server.
When I open SQL Server management configuration and use the username and login specified in my connection string it allows me to connect and access the database without issue. But doing it through python/pyodbc (again, without changing any code that was working last week) I get the following error.
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'Warehouse'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'Warehouse'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
The connection string I am using is:
conn = pyodbc.connect(f'Driver={driver_name}; '
'Server=192.168.1.x\SERVER-NAME\SQLEXPRESS,1433;'
'Database=Warehouse;'
'Integrated Security=False;'
'Trusted_Connection=no;'
'UID=Warehouse;'
'PWD=passwordhere;'
'pool_pre_ping=True;'
'pool_recycle=3600;',
timeout=1
)
I have restarted the VM the SQL server is hosted on and it did not resolve it. Any help is appreciated.
edit:
I have my python application set to re-attempt to connect to the SQL server every ~5 minutes, so that if the connection ever times out (which is does after a while) it will re-initiate the connection. Could the warehouse user be blocked somehow over the network after too many failed login attempts to the server while it was offline?
Looking through the SQL Server Configuration Manager, the TCP Port was reset to something in the 4x,xxx range. I set it to 1433 like it was before and everything works now.

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;"

Connecting Python to SQL Server

I work for a nonprofit and I am trying to establish a connection from python scripts to our sql server (Microsoft SQL Server Management Studio 18) as a proof of thought and I am running into an error found below. I cannot find a solution and I have searched across StackOverflow and have tried different arrangements of codes to no success. Underneath the error is my code to connect to sql using pyodbc. I have been successful establishing a remote connection to sql on R (RODBC package) but trying to move to python. Is this a security issue on the SQL server side or am I missing a syntax? For security purposes, I filled in the username and password with fill-ins. Any help would be great!
('HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open server "foodbankcenc.org" requested by the login. The login failed. (40532) (SQLDriverConnect); [HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open server "foodbankcenc.org" requested by the login. The login failed. (40532)')
import pyodbc
conn = pyodbc.connect(Driver='{ODBC Driver 17 for SQL Server}',
Server= 'fbnc.database.windows.net',
Database= 'FBData',
user ='user',
password= 'password')
cursor = conn.cursor()
cursor.execute('SELECT * FROM table_name')
for i in cursor:
print(i)

Python keeps escape character in variable

I want to connect to SQL Server using PyODBC.
connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
The username should be "MYCOMPANY\username" so, I have set the variables username like this: username = "MYCOMPANY\\username".
The problem is that im getting this error :
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'MYCOMPANY\\username'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'MYCOMPANY\\username'. (18456)")
I don't understand why the string keeps the "\\".
Anyone can help me up with that please ?
You can use .encode then .decode:
username.encode().decode('unicode_escape')

pyodbc- connection failure to SQL Server

I have been trying to connect to Microsoft SQL Server. I have an ODBC connection set up and the test is successful. I am not using Windows Authentication to connect to SQL Server but it keep getting this error:
Cannot be used with Windows authentication
InterfaceError: ('28000', '[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (18452) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (18452); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
Here is my code:
import pyodbc
cnxn = pyodbc.connect(Driver='{SQL Server}',
Server='servername.abc.xyz.co.com',
username = 'user_xyz',
password = 'abcdfgh')
I am using Windows 7. Please help me debug this problem
Thanks
I was able to solve this by defining the dsn connection as below:
dsn="DRIVER={SQL
SERVER};server=ip_address_here;database=db_name_here;uid=user;pwd=password"
This worked and I was able to connect and query the sql server.
This is how I do it and it works:
import pyodbc
server_name = "server_name"
db_name = "db_name"
server = "Server="+str(server_name)
db = "Database="+str(db_name)
key = "Driver={SQL Server Native Client 11.0};"+server+";"+db+";"+"Trusted_Connection=yes;"
cnxn = pyodbc.connect(key)

Categories

Resources