Linux下Python使用jdbc方式连接Oracle
1、在Pycharm中安装cx_Oracle包

3、解压instantclient,本人测试中解压路径为 /home/instantclient_11_2,linux命令:unzip instantclient-basic-linux.x64-11.2.0.4.0.zip

5、在linux中,添加instantclient到运行时链接路径,linux命令:sudo sh -c "echo /opt/oracle/instantclient_12_2 > /etc/ld.so.conf.d/oracle-instantclient.conf" sudo ldconfig
6、配置文件中,配置Oracle_home,vi /etc/profileexport ORAC雉搽妤粲LE_HOME=/home/instantclient_11_2export PATH=$ORACLE_HOME:$PATHexport TNS_ADMIN=$ORACLE_HOME/network/adminexport LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATHexport NLS_LANG='SIMPLIFIEDCHINESE_CHINA.AL32UTF8'然后 source /etc/profile,使配置文件生效。

8、Python代码连接Oracle数据库第一种写法:import cx_Oracleoracle_conn=cx_Oracle.connect('username/password@server:port/database')oracle_cursor=oracle_conn.cursor()oracle_cursor.execute('SELECT * FROM yourtables')print(oracle_cursor.fetchall())
9、Python代码连接Oracle数据库第二种写法:oracle_u衡痕贤伎sername=usernameoracle_pwd=password茑霁酌绡host=yourhostport=yourportdbname=yourdatabasedsn=cx_Oracle.makedsn(host,port,dbname)oracle_conn=cx_Oracle.connect(oracle_username,oracle_pwd,dsn)oracle_cursor=oracle_conn.cursor()oracle_cursor.execute('SELECT * FROM yourtable')print(oracle_cursor.fetchall())