Python keeps escape character in variable - python

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

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.

How do I figure out the correct DRIVER string for ODBC SQL connection (in python)

I have a python script that is using sqlAlchemy to connect to a SQL Server DB. We moved the DB from a local server in the closet to AWS.
When running from a mac, I have:
'DRIVER=/usr/local/lib/libtdsodbc.so',
and that works for both closet and AWS
on a Windows machine, for the closet the connection contained:
'DRIVER=SQL Server',
which gives the error message:
(pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Cannot generate SSPI context (0) (SQLDriverConnect); [HY000] [Microsoft][ODBC SQL Server Driver]Cannot generate SSPI context (0)')
searching around, tells me that maybe I need a new driver, so:
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows recommends:
'DRIVER={{ODBC Driver 18 for SQL Server}}'
(Because I'm formatting into the connection string, so I need to escape the {})
gives:
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)')
alternatively,
'DRIVER=ODBC Driver 18 for SQL Server',
gives:
(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
This SQLAlchemy engine works:
Engine(mssql+pyodbc://cam-tls1:1433/services_vars?driver=SQL Server)
This one does not:
Engine(mssql+pyodbc://P-CEP-SQL:1433/services_vars?driver={ODBC Driver 18 for SQL Server})
it gives a syntax error on the DRIVER keyword...
Do I need to download a new library? or do I need to specify the driver differently?
I'm still not exactly sure how I was SUPPOSED To find this out, but this works:
'DRIVER=ODBC+Driver+17+for+SQL+Server',
even though 18 appears to be current, it doesn't work for 18. but it does work for 17.

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)

Cannot open database [Microsoft][ODBC Driver 13 for SQL Server] using python

I got the following error ,[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for (); [] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open database "xxxxx" requested by the login. The login failed. ()').
i have tried all the combinations. I have also checked the version of ODBC driver ( 13 odbc and 17 odbc with 64bytes) and python supportive with 64 bytes.
conn_str = (
'DRIVER={ODBC Driver 13 for SQL Server};'
'SERVER=xxxxx;'
'DATABASE=xxxxx;'
'Trusted_Connection=yes;'
'username = xxxx;'
'password = psswrd;'
)
cxn = pyodbc.connect(conn_str)

pyodbc InterfaceError

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.

Categories

Resources