Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

How might I access Oracle from Python? I have also downloaded a cx_Oracle MSI installer, however, Python can't import the library. 

I get the accompanying error:

import cx_Oracle

Traceback (most recent call last):

  File "<pyshell#1>", line 1, in <module>

    import cx_Oracle

ImportError: DLL load failed: The specified module could not be found.

Can anyone please help me.

1 Answer

0 votes
by (26.4k points)

Here is the manner by which my code resembles. It additionally tells an illustration of the best way to utilize query parameters with the help of a dictionary. It also works on  utilizing Python 3.6:

import cx_Oracle

CONN_INFO = {

    'host': 'xxx.xx.xxx.x',

    'port': 12345,

    'user': 'SOME_SCHEMA',

    'psw': 'SECRETE',

    'service': 'service.server.com'

}

CONN_STR = '{user}/{psw}@{host}:{port}/{service}'.format(**CONN_INFO)

QUERY = '''

    SELECT

        *

    FROM

        USER

    WHERE

        NAME = :name

'''

class DB:

    def __init__(self):

        self.conn = cx_Oracle.connect(CONN_STR)

    def query(self, query, params=None):

        cursor = self.conn.cursor()

        result = cursor.execute(query, params).fetchall()

        cursor.close()

        return result

db = DB()

result = db.query(QUERY, {'name': 'happy'})

Interested to learn the concepts of Python in detail? Come and join the python course to gain more knowledge in Python

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...