以下是Python3.7.4 连接Oracle 11g并查询数据的实例,代码如下:
import cx_Oracle
def main():
# 1.建立连接
conn = cx_Oracle.connect('scott', 'tiger', 'IP地址:1521/ORCL')
# 2.创建游标
cursor = conn.cursor()
# 3.执行SQL
res = cursor.execute('select empno,ename from emp')
# 4.获取数据
data = res.fetchall()
for empno, ename in data:
print('=' * 30)
print("员工编号:" + str(empno), "员工姓名: " + ename)
# 5.关闭游标
cursor.close()
# 6.关闭连接
conn.close()
if __name__ == '__main__':
main()
运行截图: