python连接mysql、oracle数据库

python版本:3.10.5
mysql版本: 8.0.27
oracle版本:oracle 12c

一、python连接mysql数据库

  1. 安装第三方依赖PyMySQL, 终端执行如下命令:
pip install PyMySQL
  1. PyMySQL使用
import pymysql

config = {
        'host': '127.0.0.1',
        'port': 3306,
        'user': 'root',
        'password': 'root',
        'database': 'mysql',
}
# 获取连接
connection = pymysql.connect(**config)
# 获取游标
cursor = connection.cursor()
# 执行sql
cursor.execute("select now() from dual")
# 提取结果
print(cursor.fetchone())
# 关闭资源
cursor.close()
connection.close()

# 使用with语句可以不用手动关闭资源
with pymysql.connect(**config) as conn:
    with conn.cursor() as cur:
        cur.execute("select now() from dual")
        print(cur.fetchone())

二、python链接oracle数据库

  1. 安装第三方依赖oracledb (cx_oracle),终端执行如下命令:
pip install oracledb

官方文档:https://python-oracledb.readthedocs.io/en/latest/user_guide/installation.html
2. oracledb使用

import oracledb

config = {
    'user': 'system',
    'dsn': '127.0.0.1:1521/orcl',
    'password': '123456'
}

with oracledb.connect(**config) as connection:
    with connection.cursor() as cursor:
        cursor.execute("select sysdate from dual")
        print(cursor.fetchone())
# where子句参数赋值
with oracledb.connect(**config) as connection:
    with connection.cursor() as cursor:
        cursor.execute("select sysdate from dual where 1=:param", [1])
        print(cursor.fetchone())

python连接mysql、oracle数据库

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值