cx_Oracle connection by python3.5 - oracle

I've tried several attempt to connect Oracle DB but still unable to connect. Following is my code to connect. However, I could connect Oracle DB through the terminal like this:
$ sqlplus64 uid/passwd#192.168.0.5:1521/WSVC
My evironment: Ubuntu 16.04 / 64bit / Python3.5
I wish your knowledge and experience associated with this issue to be shared. Thank you.
import os
os.chdir("/usr/lib/oracle/12.2/client64/lib")
import cx_Oracle
# 1st attempt
ip = '192.168.0.5'
port = 1521
SID = 'WSVC'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
# dsn_tns = cx_Oracle.makedsn(ip, port, service_name=SID)
db = cx_Oracle.connect('uid', 'passwd', dsn_tns)
cursor = db.cursor()
-------------------------------------------------
# 2nd attempt
conn = "uid/passwd#(DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.5)(PORT=1521)))(CONNECT_DATA=(SID=WSVC)(SRVR=DEDICATED)))"
db = cx_Oracle.connect(conn)
cursor = db.cursor()
------------------------------------------------------
# ERROR Description
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle

The error "unable to acquire Oracle environment handle" is due to your Oracle configuration being incorrect. A few things that should help you uncover the source of the problem:
when using Instant Client, do NOT set the environment variable ORACLE_HOME; that should only be set when using a full Oracle Client or Oracle Database installation
the value of LD_LIBRARY_PATH should contain the path which contains libclntsh.so; the value you selected looks like it is incorrect and should be /usr/lib/oracle/12.2/client64/lib instead
you can verify which Oracle Client libraries are being loaded by using the ldd command as in ldd cx_Oracle.cpython-35m-x86_64-linux-gnu.so

Related

ODBC Data Source Connection Successful but TNS Unresolved in VBScript

I am trying to make a connection to Oracle DB from VBScript. I've added all the environment variables and the code can locate the file of TNSNAMES.ora but cannot resolve the connection.
When I test the connectivity from the ODBC Data Source Administrator, I get a 'Success' result, but I still cannot access this from the code.
con.ConnectionString = "DSN=DATA;Uid=wh;Pwd=pwd;"
con.Open 'This is where it fails
I have tried many connection strings but all fail
OracleConnString = "Driver={Oracle in instantclient_18_3};server=server;database=db;trusted_connection=Yes;"
DB_CONN_STRING = "Driver={Oracle in instantclient_18_3}; " & _
"(DESCRIPTION=" & _
"(ADDRESS = (PROTOCOL = TCP)(HOST = host)(PORT = port)" & _
"(CONNECT_DATA=(SERVICE_NAME=srv_name)))"
CONN_STRING = "CONNECT wh/pwd#//host/db"
How can I connect to the database from the script?
You mix several topics.
You hide the DSN name and the "Data Source Name", this makes it impossible for use to provide you the correct connection string.
If you like to use the ODBC DSN then the connection string must be
DSN=▇▇▇▇DATA;Uid=myUsername;Pwd=myPassword
However, you must create a System DSN, not a User DSN (see ODBC DSN). However, typically you don't use the ODBC DSN because you have to create the DSN on the target machine which is additional configuration work.
Connection string without DSN would be
Driver={Oracle in instantclient_18_3};Dbq=?????;Uid=myUsername;Pwd=myPassword;
or
Driver={Oracle in instantclient_18_3};Server=?????;Uid=myUsername;Pwd=myPassword;
see https://www.connectionstrings.com/oracle-in-oraclient11g_home1/
If you use full DB name like (DESCRIPTION=... then you don't need any tnsnames.ora file. The purpose of this file is to resolve an alias to this full DB name.
Ensure that the ODBC driver is the same architecture, i.e. 32-bit or 64-bit as your VBS environment.
Either use %windir%\system32\odbcad32.exe + %windir%\system32\cscript.exe
or %windir%\SysWOW64\odbcad32.exe + %windir%\SysWOW64\cscript.exe

RobotFramework Oracle DB Connection Issue

I'm trying to check if the oracle DB connection using the Keyword:
Connect To Database Using Custom Params
Following libraries are imported:
Database Library
JayDeBe API
This this is the connection string used:
'oracle.jdbc.driver.OracleDriver', 'jdbc:oracle:thin:#//DBHostname:Port/DBName', ['user', 'pass']
We do not get any response that can identify if the connection is established or rejected.
We see this message in RIDE:
'oracle.jdbc.driver.OracleDriver', 'jdbc:oracle:thin:#//DBHostName:Port/DBName', ['user', 'pass']
20170927 17:07:54.438 : INFO : Executing : Connect To Database Using Custom Params : jaydebeapi.connect(db_api_2.connect('oracle.jdbc.driver.OracleDriver', 'jdbc:oracle:thin:#//DBHostName:Port/DBName', ['user', 'pass']))
Can someone please help us?
We found the solution: In our case we have python 2.7(32bit) therefore we need the following:
1. Oracle Instant Client Downloads for Microsoft Windows (32-bit). you can download it from here: http://www.oracle.com/technetwork/topics/winsoft-085727.html
Set the variable path in environment variable for the Oracle Instant client.
Download cx_Oracle API (32bit- cx_Oracle-6.0.2-cp27-cp27m-win32.whl (md5) ) from the location:https://pypi.python.org/pypi/cx_Oracle/
Open command prompt and run: pip install cx_Orcle --upgrade
Open the RIDE and use the following keyword:
Connect To Database Using Custom Params cx_Oracle 'user', 'password', 'DBHOSTNAME:PORT/DBNAME'

How to load table from SQL server using H2o in R?

I try to load table into R using h2o but had the following error
my_data <- h2o.import_sql_table(my_sql_conn, table, username, password)
ERROR: Unexpected HTTP Status code: 500 Server Error (url = http://localhost:54321/99/ImportSQLTable)
java.lang.RuntimeException [1] "java.lang.RuntimeException: SQLException: No suitable driver found for jdbc:mysql://10.140.20.29/MySQL?&useSSL=false\nFailed to connect and read from SQL database with connection_url: jdbc:mysql://10.140.20.29/MySQL?&useSSL=false"
Can someone help me with this? Thank you so much!
You need a supported JDBC (Build on JDBC 42 Core) driver to connect from H2O to SQL Server. You can download Microsoft JDBC Driver 4.2 for SQL Server from the link below first:
https://www.microsoft.com/en-us/download/details.aspx?id=54671
After that please follow the article below to first test JDBC driver from R/Python H2O client and then connect to your database:
https://aichamp.wordpress.com/2017/03/20/building-h2o-glm-model-using-postgresql-database-and-jdbc-driver/
Above article is for postgres however you can use it with SQL server using an appropriate driver.
For Windows, remember to use ; instead : for the -cp argument.
java -Xmx4g -cp sqljdbc42.jar;h2o.jar water.H2OApp -port 3333
water.H2OApp is the main class in h2o.jar.
Important Note: SQL Server is not supported so far( August/2017).
You may use MariaDB to load datasets:
From Windows console:
java -Xmx4G -cp mariadb-java-client-2.1.0.jar;h2o.jar water.H2OApp -port 3333
Note. For Linux, replace ";" with ":"
From R:
sqlConn <- "jdbc:mariadb://10.106.7.46:3306/DBName"
userName <- "dbuser"
userPass <- "dbpass."
sql_Query <- "SELECT * FROM dbname.tablename;"
mydata <- h2o.import_sql_select( sqlConn, sql_Query, userName, userPass )

Convert from WE8MSWIN1252 to UTF-8 in MariaDB Engine Connect

I'm connecting to an Oracle DB with WE8MSWIN1252 encoding from a webserver that displays content in UTF-8 using MariaDB Connect Engine.
The code used for connecting the tables is like this:
CREATE TABLE mytable ENGINE=CONNECT TABLE_TYPE=ODBC TABNAME='oracle_table_name' CONNECTION='DSN=orcl;UID=someuser;PWD=passwd';
I know I have to pass somewhere the expected charset, but not sure about the syntax using this engine.
I'm able to correctly display the characters connecting directly from php using this code:
oci_connect('someuser', 'passwd', 'host', 'AL32UTF8');
The odbc.ini file looks like this:
[orcl]
Driver = Oracle 12c ODBC driver
Servername = host
I have tried putting Charset = UTF-8 in this file but it didn't work.
After many tries and a lot of investigation I understood that it was a matter of the environment setup.
I finally solved it writing the next line in /etc/init.d/mysql:
NLS_LANG=SPANISH_COLOMBIA.WE8MSWIN1252 ; export NLS_LANG
And to connect from isql the next line was needed in /root/.bash_profile:
NLS_LANG=SPANISH_COLOMBIA.AL32UTF8 ; export NLS_LANG
I was on CentOS 6, so I suppose in other environments this could not work.

Connecting to Oracle DB using Ruby

I am stuck with connecting to Oracle DB, have read lots of stuff but no help on result.
I have remote Oracle DB, I am connecting to it using DBVisualizer setting connection like this:
DB Type : Oracle
Driver (jdbc) : Oracle thin
Database URL: jdbc:oracle:thin:#10.10.100.10:1521/VVV.LOCALDOMAIN
UserIdf: SomeUser
Pass: SomePass
Connection works ok.
What I do in Ruby is :
require 'oci8'
require 'dbi'
...
conn = OCI8.new('SomeUser','SomePass','//10.10.100.10:1521/VVV.LOCALDOMAIN')
...
What I get is:
ORA-12545: Connect failed because target host or object does not exist
oci8.c:360:in oci8lib.so
the third parameter needs to be the TNS hostname, if you use SQL plus it is also the third parameter in the connectstring, you can find it also in the tnsnames.ora file in the oracle maps
in SQLPlus : connect user/password#hostname;
in oci8 : conn = OCI8.new('SomeUser','SomePass',hostname)
Here a working sample, obfuscated the parameters of course
require 'oci8'
oci = OCI8.new('****','***','****.***')
oci.exec('select * from table') do |record|
puts record.join(',')
end

Resources