python installs the oracle extension and database connection method
- 2020-05-26 09:35:35
- OfStack
This article illustrates the python installation of the oracle extension and the database connection method. I will share it with you for your reference as follows:
Download:
cx_Oracle download address: http:// cx-oracle.sourceforge.net /
instantclient - basic download address: http: / / www oracle. com technetwork/database features/instant - client/index - 097480. html
window environment:
python27 oracle10
Software required:
cx_Oracle-5.1.2-10g.win32-py2.7.msi
instantclient-basic-win32-10.2.0.4.zip
1. Directly double-click the msi file to install cx_Oracle;
2. Unzip instantclient-basic-win32-10.2.0.4.zip and copy all the obtained.dll files to F:\Python27\Lib\ site-packages
linux environment:
python26 orracle10
Software required:
cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
basic-10.2.0.4.0-linux-x86_64.zip
1. rpm -ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
2. (reference https here: / / www. ofstack. com article / 106295 htm)
Setting environment variables
vi /root/.bash_profile
Add the following two lines:
export ORACLE_HOME=/usr/local/instantclient_10_2
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
Run source /root/.bash_profile for the changes to take effect
Create symbolic links for this link library
cd $ORACLE_HOME
ln -s libclntsh.so.x.x libclntsh.so
Reinstall cx_Oracle
Note the --nodeps parameter, otherwise the above error will be reported
[root@BJ-UPDATE-01 ~]# rpm -ivh --nodeps cx_Oracle-5.0.1-10g-py24-1.x86_64.rpm
Version #5.0.3 does not include the --nodeps parameter
Testing:
#Python
>>> import cx_Oracle
>>> db = cx_Oracle.connect('user/psw@114.242.113.91:1521/orcl')
>>> print db
<cx_Oracle.Connection to user@114.242.113.91:1521/orcl>
>>> cr=db.cursor()
>>> cr.execute("select * from LOGININFO")
<__builtin__.OracleCursor on <cx_Oracle.Connection to user@114.242.113.91:1521/orcl>>
>>> rs=cr.fetchall()
>>> print rs
[('40288a8542746fd90142746fdbb50001', 'cccccc', 1, 1, None, None, None), ('40288a85427474b601427474b8270001', 'eeee', 1, 1, None, None, None), ('40288a854273bce0014273bee6310002', 'dddddd', 0, 0, None, None, None), ('40288a854274532d014274532f600001', 'cccccc', 1, 1, None, None, None), ('40288a8542747c750142747c77ac0001', 'eeee', 1, 1, None, None, None), ('40288a8542744fb30142744fb5e90001', 'cccccc', 1, 1, None, None, None)]
>>>
For more information about Python, please visit our site: Python common database operations skills summary, Python coding skills summary, Python pictures skills summary, "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"
I hope this article is helpful for you to design Python program.