python: Connecting to Oracle 11g Database in Python

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:python -m pip install oracledb
# python -m pip install cx_Oracle --upgrade
# pip install cx_Oracle
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, poostgreSQL 17.0
# Datetime  : 2024/12/21 22:14
# User      : geovindu
# Product   : PyCharm
# Project   : Pysimple
# File      : oracledemo.py
# explain   : 学习
import getpass
import oracledb
import cx_Oracle
 
# 连接到Oracle数据库
connection = cx_Oracle.connect(user="SCOTT", password="88888", dsn="localhost/ORCL")
 
# 创建游标
cursor = connection.cursor()
 
# 执行查询
cursor.execute("SELECT * FROM DEPT")
 
# 获取查询结果
result = cursor.fetchall()
 
# 打印查询结果
for row in result:
    print(row)

  

 

 

 

 

Oracle SQL Developer Downloads | Oracle 中国

https://www.oracle.com/cn/database/sqldeveloper/technologies/download/

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:python -m pip install oracledb
# python -m pip install cx_Oracle --upgrade
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm Community Edition 2024.3 python 3.11
# OS        : windows 10
# Datetime  : 2024/12/22 22:33
# User      : geovindu
# Product   : PyCharm  Community
# Project   : ictsimple
# File      : oracledemo.py
# explain   : 学习
 
import cx_Oracle
 
 
class dept(object):
    """
 
    """
    def __init__(self):
        """
 
        """
        self.__idno=None
        self.__deptname=None
        self.__localcity=None
 
    @property
    def idno(self):
        """
 
        :return:
        """
        return self.__idno
 
    @idno.setter
    def idno(self,no:str):
        """
 
        :param no:
        :return:
        """
        self.__idno=no
 
    @property
    def deptname(self):
        """
 
        :return:
        """
        return self.__deptname
 
    @deptname.setter
    def deptname(self,name:str):
        """
 
        :param name:
        :return:
        """
        self.__deptname=name
 
    @property
    def localcity(self):
        """
 
        :return:
        """
        return self.__localcity
 
    @localcity.setter
    def localcity(self,city:str):
        """
 
        :param city:
        :return:
        """
        self.__localcity=city
 
 
 
# 连接到Oracle数据库
connection = cx_Oracle.connect(user="SCOTT", password="88888", dsn="localhost/orcl.docker.internal")
# 创建游标
cursor = connection.cursor()
# 执行查询
cursor.execute("SELECT * FROM DEPT")
# 获取查询结果
result = cursor.fetchall()
dt=[]
# 打印查询结果
for idno,name,city in result:
    print(idno,name,city)
    info=dept()
    info.idno=idno
    info.deptname=name
    info.localcity=city
    dt.append(info)
print("****************")
for dd in dt:
    print(dd.idno,dd.deptname,dd.localcity)

  

全局数据库名的名称要记着,程序代码连接数据库有关联。

 

posted @   ®Geovin Du Dream Park™  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-12-21 CSharp: Observer Pattern in donet core 6
2013-12-21 asp: AJAX Database
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示