Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 234 Big Data Appliance
- 1.9K Data Science
- 449.7K Databases
- 221.5K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.8K SQL & PL/SQL
- 21.2K SQL Developer
- 295.3K Development
- 17 Developer Projects
- 138 Programming Languages
- 292K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 27 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 157 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 387 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1K Español
- 1.9K Japanese
- 230 Portuguese
ORA: 12154 error while connecting to Oracle 11g database using python 3.6

import cx_Oracleprint('connection start')db_connection = cx_Oracle.connect("jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = <Host1>)(PORT = <port_number>)) (ADDRESS = (PROTOCOL = TCP)(HOST = <Host2>)(PORT = <port_number>)) (FAILOVER=true)(LOAD_BALANCE=true) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = <service_name>) (FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))))", "<username>", "<password>")print(db_connection)print('connection successful')
I'm trying to connect to Oracle 11g database using the above python 3.6 code. However, I'm encountering the following error.
DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
I'm able to successfully connect to same oracle 11g database by using Oracle-SQL-Developer version 17 (with the same connection string).
So, I'm wondering if there is any issue with my code or does cx_Oracle needs supporting drivers etc. Any help would be much appreciated.
Best Answer
-
cx_Oracle can't use JDBC thin to connect. You need to use an Oracle Client (like Instant Client which you can download from here: Oracle Instant Client Downloads). Then, you can use EZ connect as in the following:
db_connection = cx_Oracle.connect("user/[email protected]:port/service")
You replace the bits in the string with the values for your configuration. A default configuration might look like this:
db_connection = cx_Oracle.connect("cx_Oracle/[email protected]/orclpdb")
Essentially, any connect string that works with SQL*Plus should also work with cx_Oracle. You just can't use JDBC thin!
Hope that helps.
Anthony
Answers
-
cx_Oracle can't use JDBC thin to connect. You need to use an Oracle Client (like Instant Client which you can download from here: Oracle Instant Client Downloads). Then, you can use EZ connect as in the following:
db_connection = cx_Oracle.connect("user/[email protected]:port/service")
You replace the bits in the string with the values for your configuration. A default configuration might look like this:
db_connection = cx_Oracle.connect("cx_Oracle/[email protected]/orclpdb")
Essentially, any connect string that works with SQL*Plus should also work with cx_Oracle. You just can't use JDBC thin!
Hope that helps.
Anthony
-
Anthony, Thank you for your suggestion and I'm able to successfully establish a database connection now. I stripped down my complex connection string into "user/[email protected]:port/service" format and that solved the problem. Because my initial connection string has two hosts, i prepared two connections string one for each host in the suggested format and found out that one of the host connection is no longer valid and that is throwing me an error "ORA-12154". I appreciate your quick response with a working solution.
-
You're welcome. Glad to be of help!