Python操作Mysql Postgresql Oracle数据库案例
本文最后更新于 393 天前,其中的信息可能已经有所发展或是发生改变,纠正错误请发送邮件到<baigeixiangcai@gmail.com>。
建议在Python3.*下使用

使用前需要先用pip安装以下包

Mysql

pip install PyMySQL

Postgresql

pip install psycopg2

Oracle

pip install cx_Oracle 
cx_Oracle 执行报错cx_Oracle.DatabaseError: DPI-1047: Oracle Client library must be at version 11.2 or higher
参考地址:https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#install-cx-oracle

案例代码

# coding=UTF-8

import cx_Oracle
import psycopg2
import pymysql


# mysql连接
def mysql(instancetype, host, port, database, user, password, charset, sql):
    """
    host        连接地址\n
    port        端口\n
    database    数据库名\n
    user        用户名\n
    password    密码\n
    charset     编码\n
    sql         SQL语句\n
    """
    try:
        # 打开数据库连接
        db = pymysql.connect(host=host,
                             user=user,
                             password=password,
                             port=int(port),
                             database=database,
                             charset=charset)
        try:
            # 使用 cursor() 方法创建一个游标对象 cursor
            cursor = db.cursor()
            # 使用 execute()  方法执行 SQL 查询
            cursor.execute(sql)
            # 使用 fetchone() 方法获取单条数据.
            result = cursor.fetchall()
            print(result)
        except Exception as e:
            # 获取数据异常
            print("Error: ", e)
        finally:
            # 关闭数据库连接
            db.close()
    except Exception as e:
        # 数据库连接异常
        print("Error: ", e)
    pass


# postgresql连接
def postgresql(instancetype, host, port, database, user, password, charset, sql):
    """
    host        连接地址\n
    port        端口\n
    database    数据库名\n
    user        用户名\n
    password    密码\n
    charset     编码\n
    sql         SQL语句\n
    """
    try:
        # 打开数据库连接
        db = psycopg2.connect(host=host,
                              user=user,
                              password=password,
                              port=int(port),
                              database=database,
                              client_encoding=charset)

        try:
            # 使用 cursor() 方法创建一个游标对象 cursor
            cursor = db.cursor()
            # 使用 execute()  方法执行 SQL 查询
            cursor.execute(sql)
            # 使用 fetchone() 方法获取单条数据.
            result = cursor.fetchall()
            print(result)
        except Exception as e:
            # 获取数据异常
            print("Error: ", e)
        finally:
            # 关闭数据库连接
            db.close()
    except Exception as e:
        # 数据库连接异常
        print("Error: ", e)
    pass


def oracle(instancetype, host, port, database, user, password, charset, sql):
    try:
        user = user
        password = password
        host = host + ":" + port
        service_name = database
        conn_str = f"{user}/{password}@{host}/{service_name}"
        connect = cx_Oracle.connect(conn_str)
        try:
            cursor = connect.cursor()
            cursor.execute(sql)
            result = cursor.fetchall()
            print(result)
        except Exception as e:
            print("Error1: ", e)
        finally:
            connect.close()
    except Exception as e:
        print("Error2: ", e)


def sql_client(instancetype, sqltype, host, port, database, user, password, charset, sql):
    if sqltype == "mysql":
        print("mysql......")
        mysql(instancetype, host, port, database, user, password, charset, sql)
    elif sqltype == "postgresql":
        print("postgresql......")
        postgresql(instancetype, host, port, database, user, password, charset, sql)
    elif sqltype == "oracle":
        print("oracle......")
        oracle(instancetype, host, port, database, user, password, charset, sql)
    else:
        print("数据库类型:" + sqltype + "未适配")
    pass

Last Updated on 2023 18 10 月

暂无评论

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇