Python 3.3 - 连接 Oracle 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19042353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:38:03  来源:igfitidea点击:

Python 3.3 - Connect with Oracle database

pythonoraclepython-3.3

提问by aIKid

Is there a module for python 3.3 to connect with Oracle Databases? Which is the easiest to use? Something like the mysql module, only works with Oracle.

是否有 python 3.3 与 Oracle 数据库连接的模块?哪个最容易使用?类似于 mysql 模块的东西,只适用于 Oracle。

Preferably version 10g, but 11g will do just fine.

最好是 10g 版本,但 11g 也可以。

采纳答案by Joyfulgrind

There is:cx_Oracle

有:cx_Oracle

# Install --> You should have oracle installed otherwise exception will be raised

pip install cx_Oracle

import cx_Oracle

con = cx_Oracle.connect('pythonhol/[email protected]/orcl')
print con.version

con.close()

http://www.orafaq.com/wiki/Python

http://www.orafaq.com/wiki/Python

http://www.oracle.com/technetwork/articles/dsl/python-091105.html

http://www.oracle.com/technetwork/articles/dsl/python-091105.html

回答by chris chang

if you're using python3

如果您使用的是 python3

pip3 install cx_Oracle

How to connet oracle and get oracle time:

如何连接oracle并获取oracle时间:

#!/usr/bin/python3
#coding=utf8


# import module
import cx_Oracle 

con = cx_Oracle.connect('username/password@databasename')

# create cursor
cursor = con.cursor()

# execute sql
cursor.execute('select sysdate from dual')

# fetch one data, or fetchall()
data = cursor.fetchone()

print('Database time:%s' % data)

# close cursor and oracle
cursor.close()
con.close()