Windows Authentication Frequently Failing with pymssql - python

I am having a problem where Windows Authentication has almost completed stopped working for pymssql 2.1.3. It was working last week, but now when I try and use my AD login, it fails almost every time with the error (20009, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (878240-SQLdb4:1433)\n'), though it does manage to succeed about 1 in every 100 attempts. I have Sql Server Management Studios installed on the server where my Python code is running from, and Windows Authentication appears to be working fine. I checked the sql log for failed logins and I couldn't see any attempts.
Here is my code:
import pymssql
conn = pymssql.connection(server='myserver',database='database1')
Any ideas to make it connect consistently?
*Edit: It appears to work more consistently if it is the first time I have tried logging in after a few minutes.

So I just had to switch to pyodbc which has better support for Windows authentication. Keep in mind that the latest release of pyodbc (4.0.21) does not support Python 3.6, so you need to use 3.5. Apparently there is a wheel for python 3.6, but I haven't tested it and it appears pip is unable to natively install it on Windows. I also had to change a few things such as the connection string and how stored procedures are called (pyodbc does not have the callproc function). Here is my connection string now:
import pyodbc
conn = pyodbc.connect(server='server',database='database',Trusted_Connection='yes', driver = '{ODBC Driver 13 for SQL Server}')

Related

ibm_db.connect string stating error with database name

I'm running python on a server that will eventually do my ETL and one of the databases that I have to connect to is running Informix (I believe its version 14). I have access to the Informix DB and have an active ODBC connection on a Windows 10 machine that connects no problem. I'm trying to get the basic connection down to the Informix Server and keep getting the following error:
Exception: [IBM][CLI Driver] SQL1013N The database alias name or database name "" could not be found. SQLSTATE=42705 SQLCODE=-1013
My code to connect:
import ibm_db
import pandas as pd
conn_str = ibm_db.connect('database=mydatabase;host=myservername;port=9088;protocol=onsoctcp;uid=myusername;pwd=mypassword','','')
df = pd.read_sql('SELECT * FROM schema.table', conn_str)
print(df)
With the error, I know the name is correct and I've tried a few different variations on the DB name as I know that its case sensitive with informix. I've also tried connecting to some of the configuration databases that are available in the drop down from the ODBC connection and everything matches up. I've also tried running it in a virtual environment and treating the server as a strictly python server. OS is Ubuntu 20.04LTS.
I tried the IfxPy install again and had an error showing a missing files/sources and an error on the use_2to3 file that was giving me issue. I managed to get it installed once I found this post: (Cannot connect to Informix DB using python) which shows one other thing that has to be declared than the GitHub page for IfxPy.
export CSDK_HOME=$INFORMIXDIR
Once I followed that and declared it on my system it compiled no problem and I've been able to extract some data. I'm still playing with it but I'm at least getting data out, now I just have to get it in a useable format. Appreciate all the help.

Issues connecting to a DB2 server using Python. Possible issue identifying the driver

I had written code to connect to a DB2 server using Python that was working fine until my company had me upgrade the IBM DB2 Driver to 11.5. Now, its no longer working and I'm getting the following error message:
SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "15" ("PROCESSING FAILURE"). SQLSTATE=08001
I verified that I have the right password. Also, I confirmed that I still have access to the Database by going through the ODBC Manager in Windows and testing a connection that way. I've checked several pages here on Stackoverflow and other sites and nothing seems to work. Most of the issues people are having on this site involve different error messages.
Here is the original code using the ibm_db module:
import ibm_db
import ibm_db_dbi
con = ibm_db_dbi.Connection(ibm_db.connect("ABC1011","rsmith","Passw123"))
When that stopped working i tried connecting using pyodbc as a connection:
import pyodbc
cnxn = pyodbc.connect('DRIVER={IBM DB2 ODBC DRIVER - DB2COPY2};Database=ABC1011;UID=rsmith; PWD=Passw123')
Again, I already verified in the ODBC Datasource Administrator (64-bit) that I am using the correct password, I have the correct database, and that I am using the correct driver's name. Does anyone know what else I may need to include to get a connection to work?
Thank you.

Connecting Python 3.x to Oracle DB

I am unable to connect to our enterprise Oracle Db using python 3/cx_Oracle.
Installed are:
python 3 -32 bit
cx_Oracle
Oracle Client 12.1.0.2.0
My connection string attempt is:
import cx_Oracle
conn = cx_Oracle.connect(user='user', password='pwd', dsn='working_dsn')
My PATH variable includes the direct path to my working Oracle library (works using SQL Dev
Error message is:
cx_Oracle.DatabaseError: DPI-1050: Oracle Client library is at version 0.0 but must be at version 11.2 or higher
I have researched the Orcale installation instructions and have found no way to connect. I have previously tried with no success, had my computer reimaged and Oracle reinstalled to ensure only one version of Oracle and still no success. I need to move from R to Python and this is the last piece I need to make the switch. I am able to connect with R using JDBC driverclass/dbConnect.
If cx_Oracle wont work, is there another option for connecting to Oracle from Python3?
Any thoughts suggestions or places to look? Other connection types used?
Thanks in advance.
pip3 install cx_Oracle
first method:
db = cx_Oracle.connect('root/root#localhost: 1523/orcl')
Second method:
db = cx_Oracle.connect('root/root#localhost: 1523/orcl')
Third method
makedsn(IP/HOST, PORT, TNSNAME)
dsn = cx_Oracle.makedsn('localhost','1523','orcl')
db = cx_Oracle.connect('root','root',dsn)
The error you are getting suggests that you have an older version of the Oracle client installed on your machine. Search the directories of your PATH environment variable for OCI.DLL. If you find an older version you'll need to remove or rename it -- just be aware that whatever application put the file there will stop working!
Another possibility is to go to a command prompt and do the following
PATH=my_path_to_instant_client;%PATH%
python test_connect.py
Finally, make sure that if your Python is 32-bit, so is your instant client installation, and if your Python is 64-bit, make sure that your instant client installation is also 64-bit.

Python and Oracle DB - "Error DPI-1050: Oracle Client library must be at version 11.2 or higher"

Some days ago, I was asked to develop a Python application capable of connecting to a Oracle DB. Since I already have an Oracle client installed (version 12.2.0), I just pip installed cx_Oracle and tried to establish a connection using below code:
import pandas as pd
import cx_Oracle
connection = cx_Oracle.connect('username/password#service_as_described_in_tnsnames.ora')
cur=connection.cursor()
input("Press Enter to continue...")
cur.execute('select* from MY_PRETTY_TABLE')
for line in cur:
print()
cur.close()
connection.close()
But when trying to run it, I got the error "DPI-1050: Oracle Client library must be at version 11.2 or higher". After googling it, I found this answer, and tried to change my code to:
my_dsn = cx_Oracle.makedsn("host",port,sid="sid")
connection = cx_Oracle.connect(user="user", password="password", dsn=my_dsn)
cursor = connection.cursor()
querystring = "SQL query"
cursor.execute(querystring)
But still, same error. It's important to notice that I have already used Oracle DB client in this same machine, to connect a DB with Power BI.
Also, if it can be helpful, my paths are setted as:
C:\instantclient_12_1
C:\Users\oracle2\product\12.1.0\client_1
C:\Users\oracle2\product\12.1.0\client_1\bin
C:\Users\oracle\product\12.2.0\dbhome_1\bin
That error implies that you have another older version of the Oracle client installed somewhere earlier in your PATH. You should do a search for OCI.DLL on your machine (using where.exe or the dir command) and either move or remove any unnecessary copies or adjust PATH as needed. Some older applications stuffed OCI.DLL in C:\Windows\System32 improperly, for example.
This problem had to do with Oracle Instant client Version 19.3.0.0.0.
I uninstalled it and installed the previous version Oracle Instant client 12.2.0.1.0 to and it worked.
https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Important things to do before you try above:
From Windows command prompt
c:> where oci.dll
make sure you find only one entry and remove the redundant ones.
Ensure you set the environment path to the newly installed client.

Unable to connect to SQL Server with Windows Authentication in a Python script on a MAC

I am trying to run a phyton script on my Mac (OS Sierra) to connect to SQL Server (2016) using my login which is a Windows AD authentication but can't seem to get it to work. It throwing an Image not found error message. However, the pytds and pyodbc module both work with another login that uses Sql Server authentication.
I am unable to even trace where the disconnect is. Can someone please advice?
Try this connection string:
conn= pyodbc.connect(r'Driver={SQL
Server};Server=yourServer;Database=yourDatabase;Trusted_Connection=yes;')
replace yourServer and yourDatabase

Categories