How can I access Oracle from Python?

后端 未结 9 568
傲寒
傲寒 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: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.

提交回复
热议问题