How can I access Oracle from Python?

后端 未结 9 502
傲寒
傲寒 2020-12-04 12:15

How can I access Oracle from Python? I have downloaded a cx_Oracle msi installer, but Python can\'t import the library.

I get the following error:

im         


        
相关标签:
9条回答
  • 2020-12-04 12:51

    You can use any of the following way based on Service Name or SID whatever you have.

    With SID:

    import cx_Oracle
    dsn_tns = cx_Oracle.makedsn('server', 'port', 'sid')
    conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns)
    c = conn.cursor()
    c.execute('select count(*) from TABLE_NAME')
    for row in c:
       print(row)
    conn.close()
    

    OR

    With Service Name:

    import cx_Oracle
    dsn_tns = cx_Oracle.makedsn('server', 'port', service_name='service_name')
    conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns)
    c = conn.cursor()
    c.execute('select count(*) from TABLE_NAME')
    for row in c:
       print(row)
    conn.close()
    
    0 讨论(0)
  • 2020-12-04 12:52

    In addition to the Oracle instant client, you may also need to install the Oracle ODAC components and put the path to them into your system path. cx_Oracle seems to need access to the oci.dll file that is installed with them.

    Also check that you get the correct version (32bit or 64bit) of them that matches your: python, cx_Oracle, and instant client versions.

    0 讨论(0)
  • 2020-12-04 12:54
    import cx_Oracle
       dsn_tns = cx_Oracle.makedsn('host', 'port', service_name='give service name') 
       conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns) 
       c = conn.cursor()
       c.execute('select count(*) from schema.table_name')
    for row in c:
       print row
    conn.close()
    

    Note :

    1. In (dsn_tns) if needed, place an 'r' before any parameter in order to address any special character such as '\'.

    2. In (conn) if needed, place an 'r' before any parameter in order to address any special character such as '\'. For example, if your user name contains '\', you'll need to place 'r' before the user name: user=r'User Name' or password=r'password'

    3. use triple quotes if you want to spread your query across multiple lines.

    0 讨论(0)
  • 2020-12-04 12:55

    Note if you are using pandas you can access it in following way:

    import pandas as pd
    import cx_Oracle
    conn= cx_Oracle.connect('username/pwd@host:port/service_name')
    try:
        query = '''
             SELECT * from dual
                 '''
        df = pd.read_sql(con = conn, sql = query)
    finally:
        conn.close()
    df.head()
    
    0 讨论(0)
  • 2020-12-04 12:57

    Ensure these two and it should work:-

    1. Python, Oracle instantclient and cx_Oracle are 32 bit.
    2. Set the environment variables.

    Fixes this issue on windows like a charm.

    0 讨论(0)
  • 2020-12-04 12:58

    If you are using virtualenv, it is not as trivial to get the driver using the installer. What you can do then: install it as described by Devon. Then copy over cx_Oracle.pyd and the cx_Oracle-XXX.egg-info folder from Python\Lib\site-packages into the Lib\site-packages from your virtual env. Of course, also here, architecture and version are important.

    0 讨论(0)
提交回复
热议问题