python连接oracle数据库

安装模块

使用第三方模块 cx_Oracle·

pip install cx_Oracle

连接方式

import cx_Oracle as cx

#第一种

con = cx.connect('root', '123456', '127.0.0.1:1521/orcl')

#第二种

con = cx.connect('root/123456@127.0.0.1:1521/orcl')

#第三种

dsn = cx.makedsn('127.0.0.1', '1521', 'orcl')
connection = cx.connect('root', '123456', dsn)

简单使用

# -*- coding: utf-8 -*-
import cx_Oracle as cx      #导入模块
con = cx.connect('root', '123456', '192.168.80.208/orcl')  #创建连接
cursor = con.cursor()       #创建游标
cursor.execute("select sysdate from dual")  #执行sql语句
data = cursor.fetchone()        #获取一条数据
print(data)     #打印数据
cursor.close()  #关闭游标
con.close()     #关闭数据库连接

oracle 数据库的操作和mysql 的pymysql 操作基本相同,可以参考pymysql使用

启动python程序

python3 main.py 
leenhem@DESKTOP-0ACGT17:/mnt/c/workspace/code/tmp/pyoracle$ python3 main.py 
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    con = cx.connect('root', '123456', '192.168.80.208/orcl')  #创建连接
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help

报错

cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: “libclntsh.so: cannot open shared object file: No such file or directory

解决方式

下载Oracle服务器端对应的Client版本

https://www.oracle.com/database/technologies/instant-client/downloads.html

我用的是 instantclient_12_2

leenhem@DESKTOP-0ACGT17:/mnt/c/workspace/code/tmp/pyoracle$ ls /mnt/c/instantclient_12_2/ -l
total 216684
-rwxrwxrwx 1 leenhem leenhem       363 Jan 26  2017 BASIC_README
-rwxrwxrwx 1 leenhem leenhem     44220 Jan 26  2017 adrci
-rwxrwxrwx 1 leenhem leenhem     57272 Jan 26  2017 genezi
-rwxrwxrwx 1 leenhem leenhem  71638263 Jan 26  2017 libclntsh.so.12.1
-rwxrwxrwx 1 leenhem leenhem   8033199 Jan 26  2017 libclntshcore.so.12.1
-rwxrwxrwx 1 leenhem leenhem   2981501 Jan 26  2017 libipc1.so
-rwxrwxrwx 1 leenhem leenhem    539065 Jan 26  2017 libmql1.so
-rwxrwxrwx 1 leenhem leenhem   6568149 Jan 26  2017 libnnz12.so
-rwxrwxrwx 1 leenhem leenhem   2218687 Jan 26  2017 libocci.so.12.1
-rwxrwxrwx 1 leenhem leenhem 124771800 Jan 26  2017 libociei.so
-rwxrwxrwx 1 leenhem leenhem    158543 Jan 26  2017 libocijdbc12.so
-rwxrwxrwx 1 leenhem leenhem    380996 Jan 26  2017 libons.so
-rwxrwxrwx 1 leenhem leenhem    116563 Jan 26  2017 liboramysql12.so
-rwxrwxrwx 1 leenhem leenhem   4036257 Jan 26  2017 ojdbc8.jar
-rwxrwxrwx 1 leenhem leenhem    240476 Jan 26  2017 uidrvci
-rwxrwxrwx 1 leenhem leenhem     74230 Jan 26  2017 xstreams.jar

配置环境变量

添加 LD_LIBRARY_PATH=/mnt/c/instantclient_12_2/

vim ~/.bashrc
export LD_LIBRARY_PATH=/mnt/c/instantclient_12_2/

使环境变量生效

source ~/.bashrc

再次运行

python 代码 连接数据库成功

9leenhem@DESKTOP-0ACGT17:/mnt/c/workspace/code/tmp/pyoracle$ python3 main.py 
(datetime.datetime(2021, 6, 28, 11, 5, 16),)

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/851.html

发表评论

登录后才能评论

评论列表(1条)