I am trying to connect to database using cx_Oracle module i am getting below error
server_IP = ipaddress:1221/xyz
try:
db = cx_Oracle.connect('username', 'password', server_IP)
print db
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 1017:
print('Please check your credentials.')
else:
print('Database connection error: %s'.format(e))
raise
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
My question do i need to install any oracle client in linux or how to configure it in linux so that i won't get this error. please help
What are the preconditions are required in linux to use cx_Oracle module
Please check the syntax of your call to cx_Oracle.connect. It takes username, password and DSN OR one argument that has it all.
For example
con = cx_Oracle.connect('username/password#ipaddress/xyz')
or to construct a full DSN:
ip = 'ipaddress'
port = 1221
SID = 'xyz'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
db = cx_Oracle.connect('username', 'password', dsn_tns)
Using Python With Oracle
To answer the first question: yes, you need to have an Oracle client installed on your machine in order to use cx_Oracle. You can install the full client but it is a lot easier to use the instant client. If you are using an RPM based Linux distribution you can use the RPMs which simplify things considerably. Go here for the instant client:
http://www.oracle.com/technetwork/database/features/instant-client/index.html
The error you are getting suggests a configuration issue, though. Make sure that the Oracle client you use and the version of cx_Oracle you use are compatible with each other. In other words, if Python is 64-bit, your Oracle client should also be 64-bit. If Python is 32-bit, your Oracle client should be 32-bit. Check the environment variables ORACLE_HOME (if using a full Oracle client), PATH and LD_LIBRARY_PATH. This is one of the advantages of using the instant client RPMs -- no environment variables are needed at compile time and none at run-time either!
One last comment: it looks like you are using EZ Connect syntax to connect to the database. The default port for the listener is 1521, not 1221. If you are using the default port you can simply leave out that section (in other words ipaddress/xyz as noted by Martin).
Related
I have to extract data from a Notes database automatically for a data pipeline validation.
With HCL Notes I was able to connect to the database, so I know the access works.
I have the following information to access the database:
host (I got both hostname and ip address), domino server name, database name (.nsf filename)
I tried the noteslib library in the below way:
import noteslib
db = noteslib.Database('domino server name','db filename.nsf')
I also tried adding the host to the server parameter instead, but it does not work.
I receive this error:
Error connecting to ...Double-check the server and database file names, and make sure you have
read access to the database.
My question is how can I add the host and the domino server name as well (if it is required)?
Notes HCL authenticates me before accessing the database using the domino server name and the .nsf file. I tried adding the password parameter to the end, but also without luck. I am on company VPN, so that also should not be an issue.
In Order for noteslib to work you need an installed and configured HCL Notes Client on that machine. Only with an installed Notes Client the needed COM registrations and the dlls to connect to Domino are present.
In addition the Notes Client and the python version you are using need to be the same bitness: If Notes Client is 32Bit then python needs to be 32Bit. If Notes Client is 64Bit (only available since 12.0.2) then python needs to be 64Bit as well.
As soon as this requirement is met, you can simply use your example by adding the password parameter as a third parameter to your command:
db = noteslib.Database('domino server name','db filename.nsf', 'yourIDPassword')
If you still get an error when connecting to the server then you might need to put the server common name and its IP address into your hosts file.
So if your Domino- Servername is
YourServer/YourOrganization
and the IP address of that server is
192.168.1.20
then you put this into your hosts:
yourserver 192.168.1.20
You can connect using com on windows.
I use this python library https://pypi.org/project/pywin32/
import win32com.client
import sys
notesServer = "Servername/Domain"
notesFile = "file.nsf"
notesPass = ""
#Connect to notes database on server
notesSession = win32com.client.Dispatch('Lotus.NotesSession')
notesSession.Initialize(notesPass)
notesDatabase = notesSession.GetDatabase(notesServer,notesFile)
I am trying to connect to the database via Python ?
I use oracle with the sqldevolper and have no problems there, but now when connecting between oracle and python the problem appears.
When I try to connect, I get the following error :
cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
I tried the script with spyder:
import cx_Oracle
con = cx_Oracle.connect(
user="abc",
password ="pass",
dsn="....")
ORA-12514 means you are connecting to a listener, but the service name you specified in your connection string, isn't known by the listener. As you omitted the definition of the connect string (from SQL Developer and Python), hard to say if there is a typo or what caused it...
On the server you can check with "lsnrctl status" on the known services... maybe that gives you a hint, what might be wrong.
Could anyone share a functional connection method to Wonderware's Historian using python3 on OSX (or linux)?
Historian is apparently a Microsoft SQL Server OLE DB (see pg102 of http://www.logic-control.com/datasheets/1/Historian/HistorianConcepts.pdf).
Another SO post suggests that the only python library available capable of connecting to an OLE DB is 'adodbapi' (Connecting to Microsoft SQL server using Python)
an attempt in code (using default RO credentials):
import adodbapi
ServerName = "ServerName"
MSQLDatabase = "Runtime"
username = "aaUser"
password = "pwUser"
conn = adodbapi.connect("PROVIDER=INSQL;Data Source={0};Database={1};trusted_connection=yes;UID={2};PWD{3};".format(ServerName,MSQLDatabase,username,password))
That gives an error:
adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",)...
The error is probably due to the absence and unavailability of the pywin32 package, which is apparently Windows only (Pywin32 (com objects) on Mac)
Tips appreciated. I highly suspect that the Microsoft vs Mac/Linux worlds just can't be bridged in this situation.
You should be able to access an MS SQL Server database (e.g. Wonderware Historian database) with Python.
Things to check:
For ODBC, "Trusted Connection" setting should be "No". Trusted connection means that it tries to use Windows Authentication to log in. You want to use a username and password instead. I think for OLE DB you set "Integrated Security = SSPI" instead.
Connection String (Username, Server Hostname/IP, Database name, Correct Port, Syntax)
Port (make sure you use the correct port - may be a non-standard port)
Firewalls - make sure that any firewalls are set up to allow access
If you're using the hostname, make sure DNS is working (e.g. can you ping the server?)
You may need to install an ODBC driver for Linux and pyodbc. ODBC is a more open standard. As you've pointed out, OLEDB is COM-based (e.g. Windows-based) so I'm not sure if it would be compatible.
I am able to access a Wonderware server using Python3 both through sqlalchemy and pyodbc on Linux and Windows - I don't have a Mac so you're on your own there. I've read that there are other drivers available, but I don't have any experience with them. If you have any suggestions here I'd be glad to hear them.
This is how I modularize the functionality of pyodbc. Essentially, I've defined a function in our code that sets up the sql engine conncetion:
def get_conn():
conn_pyodbc = pyodbc.connect(DSN=<myDSN>, UID=<user>, PWD=<pass>)
return conn_pyodbc
And I use the connection as such:
def executeQuery(sql_query):
with get_conn() as conn:
df = pd.read_sql(sql_query, conn)
Using the context manager just seems like an easier way of handling opening and closing database connections.
As far as setting up the DSN, I needed to install the Microsoft ODBC driver, which was easy enough to do by following some links I found online. After a successful installation I edited the /etc/odbc.init and /etc/odbcinst.ini files manually such that they look like this now:
$ cat /etc/odbc.ini
[myDSN]
Driver=ODBC Driver 13 for SQL Server
Description=Awesome server
Trace=No
Server=<serverIP>
and
$ cat odbcinst.ini
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.1
UsageCount=1
In my experience, you can use 'Trusted Connection', but that means that your computer will try to connect to the server using it's Windows credentials. These work if you are logged into a Windows machine that has access to the data in question. Running on Linux is a different story, so I keep to the user/password combo.
Anymore questions feel free to ask.
I'm pretty new to python. I'm trying to connect to an informix server using python and ibm_db.connect(). However I can't seem to succeed and the error messages don't help.
Using java and jdbc I can connect successfully with the following connection url:
jdbc:informix-sqli://10.20.30.40:1234/mydb:INFORMIXSERVER=foo_bar;USER=user;PASSWORD=pass;
My attempt at using ibm_db is:
ibm_db.connect('HOSTNAME=10.20.30.40;PORT=1234;DATABASE=mydb;PROTOCOL=ONSOCTCP;UID=user;PASSWORD=pass;', '', '')
But it gives an error (Exception: [IBM][CLI Driver] SQL0902C A system error occurred. Subsequent SQL statements cannot be processed. IBM software support reason code: "". SQLSTATE=58005)
I would like if possible an equivalent string I can put in as the first argument to
ibm_db.connect('', '', '')
So I can connect with python.
Please check https://code.google.com/p/ibm-db/issues/detail?id=116&can=1&q=ONSOCTCP, This might be helpful for you. If you still facing issue then you can post your query to https://groups.google.com/forum/#!forum/ibm_db for quick response.
Protocol onsoctcp is not supported by ibm_db.
Please check https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.admin.doc/ids_admin_0207.htm, This can help you to configure a DDRA (tcpip) access to your database.
I have faced the same issue with ibm_db and now I use jayDeBeApi to connect to Informix through python. it requires java JDBC driver and application is work like a charm.
https://pypi.org/project/JayDeBeApi/#:~:text=The%20JayDeBeApi%20module%20allows%20you,of%20the%20Java%20JDBC%20driver.
Your JDBC connection string points to a SQLI Informix listener, but the 'ibm_db' python module uses DRDA (IBM Data Server Driver) to connect to the Informix engine.
Informix allows both SQLI and DRDA clients (and others like MongoDB). SQLI is the 'native' Informix protocol and supports all the Informix server features and data types. DRDA is what other IBM databases use (like DB2). It does have some limitations in terms of what types you can use.
You have two options:
Configure the Informix server to listen for DRDA connections in a another port (basically, create an DBALIASES using 'drsoctcp') as described here:
https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.admin.doc/ids_admin_0207.htm
Or leave the server as it is, and use a different Python module, one that uses SQLI like 'IfxPy'
https://github.com/OpenInformix/IfxPy
I'm trying to connect to a Firebird 1.5 database that is located on a server, from my local machine with Python fdb libary.
but I'm having no luck.
the server is windows 2008 server R1 running Firebird 1.5.6 as a service. It also has a System DSN called firebird.
How can i connect to it via python? I'm using this code:
import fdb
db = fdb.connect(host='192.168.40.28', database="C:\databases\database12.GDB", user='admin', password='admin')
but it generates this result:
Traceback (most recent call last):
File "data.py", line 4, in <module>
db = fdb.connect(host='192.168.40.28', database="C:\databases\database12.GDB", user='admin', password='admin')
File "/usr/local/lib/python2.7/dist-packages/fdb/fbcore.py", line 666, in connect
"Error while connecting to database:")
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "192.168.40.28".\n- Failed to establish a connection.', -902, 335544721)
what am I doing wrong here?
Assuming that the IP 192.168.40.28 is correct my next quess would be that you don't have the port 3050 open (thats the default port for Firebird). Check your server's firewall and open the port. You can use some other port instead of 3050 by seting the RemoteServicePort parameter in the firebird.conf file, but then you have to set the port parameter in the connect method too.
As other answers have stated, check that port 3050 is open.
However, fdb only supports Firebird 2.0 or higher. For Firebird 1.5, you can use either pyodbc or pyfirebirdsql
Note that, among other issues (like not handling Firebird INTEGER datatypes properly), pyfirebirdsql isn't 100% compliant with PEP 249 -- Python DB API 2.0 as calls to Cursor.rowcount always return -1.
Edit: After posting this, I took it upon myself to write the code for pyfirebirdsql's rowcount function, so now it works as expected, instead of always returning 1. Shortly after that, the pyfirebirdsql author fixed the INTEGER issues as well.
You must to check the port open in your host as ain said.
BTW, fdb is just for firebird 2.0 and higher.
Check it out.
https://fdb.readthedocs.org/en/latest/getting-started.html#installation