Advertisement
ovalerio

Using cx_Oracle and Python 3 to connect to a DB in Oracle VM

Oct 4th, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.15 KB | None | 0 0
  1. # Accesing Oracle DB with python using the Oracle VM #
  2. # ================================================== #
  3.  
  4. # STEP 1:  Installing pip over the proxy
  5. # --------------------------------------
  6. sudo yum install install python-pip
  7.  
  8. # NOTE: It might be necessary to configure first the http proxy
  9. # In order to figure out the proxy used by the Oracle VM host
  10. # we can use the following command in Windows 10: netsh winhttp show proxy
  11. # REF: https://superuser.com/questions/346372/how-do-i-know-what-proxy-server-im-using
  12. # C:\Users\Alfa>netsh winhttp show proxy
  13. # Aktuelle WinHTTP-Proxyeinstellungen:
  14. #    Proxyserver     :  alfaproxy:3128
  15.  
  16. # STEP 2: Exporting the proxy to the user environment
  17. # ----------------------------------------------------
  18. # In case the Oracle VM is not able to connect to the internet in order to download
  19. # packages, we have to export the proxy settings to the environment.
  20.  
  21. [oracle@localhost oracle]$ export https_proxy=alfaproxy:3128
  22.    
  23. # STEP 3: Installing python3 and PIP
  24. # -----------------------------------
  25. # Oracle VM does not come preloaded with python3-pip so I had to install that one
  26. # REF: https://superuser.com/questions/1137207/cannot-install-python-pip-with-yum  
  27.  
  28. [oracle@localhost oracle]$ sudo yum install python3 python3-pip
  29.  
  30. # STEP 4: Install cx_Oracle from within Python using PIP
  31. # -------------------------------------------------------
  32. # The final required step is to install cx_Oracle using PIP
  33. # REF: https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html
  34.  
  35. [oracle@localhost oracle]$ sudo python3 -m pip install cx_Oracle
  36. Installing collected packages: cx-Oracle
  37. Successfully installed cx-Oracle-7.2.3
  38.  
  39. # STEP 5: Testing Oracle CX
  40. # --------------------------
  41. # To test the connection to the database within python you can type the following
  42.  
  43. [oracle@localhost oracle]$ python3
  44. Python 3.6.8 (default, Aug  7 2019, 08:02:28)
  45. [GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)] on linux
  46. Type "help", "copyright", "credits" or "license" for more information.
  47. >>> import cx_Oracle
  48. >>> connection = cx_Oracle.Connection('hr', 'oracle', 'localhost:1521/orcl')
  49. >>> connection.version
  50. '19.3.0.0.0'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement