Python + cx_oracle connection failed - python

I have 2 servers:
1st - Oracle Server, 2nd - Server with SQL Developer.
SQL Developer has connection with Oracle, it works good.
I am trying to get connection to Oracle wia python + cx_oracle, i have the following code:
import cx_Oracle
try:
dsnStr = cx_Oracle.makedsn("Oracle_server_ip", "1521", "Oracle_server_sid")
con = cx_Oracle.connect(user="Oracle_user", password="Oracle_password", dsn=dsnStr)
print ('CONNECTED TO ORACLE, VER: ' + con.version)
cur = con.cursor()
except:
print ('Connection Failed')
It works good on the server, where Oracle is located.
But it doesn't work on another server, where SQL Developer is located.
Can you help me, please?

First, I would suggest upgrading to cx_Oracle 6 as the error message that is returned is likely going to be better. You can do that via this command:
python -m pip install cx_Oracle --upgrade
At a guess, however, you are missing an Oracle Client on the second machine. The easiest to install is the instant client which you can find here.

Related

How can I use Python 3.10 to connect to a MariaDB database on a NAS from a Linux machine?

Good day,
I have a Synology NAS DS120J hosting a MariaDB 10 (10.3.32-1040) database, on the same local network, I have a Linux machine running Ubuntu 22.04.
I am trying to connect the Linux machine to the MariaDB database using Python 3.10 and the MariaDB connector. However, it seems that I am missing a step.
When I access this database using the command line, I SSH from the Linux machine to the NAS, mentioning the port where the database is located on the NAS.
Then I populate my credentials to login to the database, this works fine.
But when using Python 3.10 and the MariaDB connector, I only provide with my credentials to login to the database, so I feel like I am missing the SSH step to the NAS, at least this is my assumption.
How could I achieve that ? Can someone please help ?
Here is my script in Python:
import mariadb
import json
import sys
with open('config.json') as config_file:
config = json.load(config_file)['mariadb']
try:
conn = mariadb.connect(
user=config['raspi-svr']['user'],
password=config['raspi-svr']['password'],
host=config['raspi-svr']['host'],
port=config['raspi-svr']['port'],
database="test_db"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
cur = conn.cursor()
Here is the error message I am getting when running my script:
Error connecting to MariaDB Platform: Lost connection to server at 'handshake: reading initial communication packet', system error: 0
I tried running the MariaDB connector for Python.

Connect with Pymssql / Adaptive Server connection failed (Windows.net vs Azure.com)

I am trying to connect to a new database using the below method I have used many times previously:
import pymssql
server = "servername.database.windows.net"
user = "user"
password = "pwd"
conn = pymssql.connect(server, user, password, "DB")
I am getting this error message:
MSSQLDatabaseException: (20004, b'DB-Lib error message 20004, severity 9:\nRead from the server failed (servername.database.windows.net:1433)\nNet-Lib error during Connection reset by peer (54)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (servername.database.windows.net:1433)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (servername.database.windows.net:1433)\nNet-Lib error during Operation timed out (60)\n')
One of the first suggestions from other posts is to check the install of FreeTDS. My FreeTDS is up to date and I have had no problem connecting to another server: "server2.cloudapp.azure.com". Both are azure sqlserver's so I am unsure what the connection issue could be. Why would this be? Any help is appreciated.
I searched a lot, and found a solution.
Seems that the pymssql module you installed directly with pip from pypi.org cannot work normallywith PaaS Azure SQL. You need to follow the official tutorial (Configure development environment for pymssql Python development) to download specific whl file and install it manually.
In my case, I use Windows OS with Python 3.7, so I download pymssql-2.1.4-cp37-cp37m-win_amd64.whl. cp37 for python version 3.7. win for Windows OS. amd64 for 64 bit version. You need to choose right one based on your environment.
Then install the downloaded module with pip install {your_whl_file}. After that, I can connect to my Azure SQL:
import pymssql
server = 'jackdemo.database.windows.net'
database = 'jackdemo'
username = 'jack#jackdemo'
password = '***********'
cnxn = pymssql.connect(server, username, password, database)
cursor = cnxn.cursor()
cursor.execute("select * from Users")
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
Result:
By the way, I may try to explain why you can connect to sql server with *.cloudapp.azure.com. Based on the URI path, it seems to be a DNS name for Azure VM. If so, the pymssql module would woek fine with the sql server on that VM, because it is not the PaaS Azure SQL.
Another suggestion is to use pyodbc. I see the samples from Azure SQL documentation use it. It may cause less trouble.

Python Oracle DB Connect without Oracle Client

I am trying to build an application in python which will use Oracle Database installed in corporate server and the application which I am developing can be used in any local machine.
Is it possible to connect to oracle DB in Python without installing the oracle client in the local machine where the python application will be stored and executed?
Like in Java, we can use the jdbc thin driver to acheive the same, how it can be achieved in Python.
Any help is appreciated
Installing oracle client, connect is possible through cx_Oracle module.
But in systems where the client is not installed, how can we connect to the DB.
You can use JDBC
"""
Connect from Python to Oracle via JDBC
Get JDBC-driver here: https://download.oracle.com/otn/utilities_drivers/jdbc/193/ojdbc8-full.tar.gz
Python 3.7.4
conda install -c conda-forge jaydebeapi==1.1.1 --force-reinstall -y
conda install -c conda-forge JPype1==0.6.3 --force-reinstall -y
"""
import jpype
import jaydebeapi
JHOME = jpype.getDefaultJVMPath()
jpype.startJVM(JHOME, '-Djava.class.path=/ojdbc8-full/ojdbc8.jar')
con = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver',
'jdbc:oracle:thin:user/pass#host_ip:1521:SID')
cur = con.cursor()
cur.execute('select dummy from dual')
r = cur.fetchall()
print(r[0][0])
cur.close()
con.close()
It is not correct that java can connect to oracle without any oracle provided software.
It needs a compatible version of ojdbc*.jar to connect. Similarly python's cx_oracle library needs oracle instant-client software from oracle to be installed.
Instant client is free software and has a small footprint.
Installing Oracle client is a huge pain. Could you instead create a Webservice to a system that does have OCI and then connect to it that way? This might end being a better solution rather than direct access.

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.

python3 loses connection to mysql connector during query

I am running the latest version of python 3 with the latest connector/python for Mac OS from the Oracle MySQL site. But when I test the connection with the simple commands in a python script file:
import mysql.connector
cnx = mysql.connector.connect(user='spUser', password='123',
host='127.0.0.1',
database='stellapeers_v2')
cnx.close()
I get the following errors, where the most important line is the final one that says :
"mysql.connector.errors.InterfaceError: 2013: Lost Connection to MySQL Server during query"
I have found many complaints about this issue on Google but no actual resolution.
Has anyone found out what the problem is and how to fix it?
Thank you
Here is the entire error output:

Categories