• 使用Psycopg2连接openGauss 3.0(python3)


    在使用 Python 连接 openGauss 数据库时,需要使用 Psycopg 来进行连接。openGauss 官方提供的编译包仅支持 Python 3 。若要使用 Python 2 ,则需要手动编译安装。本文首先介绍官方提供的 Python 3 版本如何正确使用,后面会再写一篇文章介绍使用 Python 2 时如何进行编译安装。
    本文测试环境使用的是 麒麟 V10 SP2 操作系统,openGauss 3.0 数据库。

    1 从openGauss官网上下载编译好的psycopg2压缩包

    下载地址:https://www.opengauss.org/zh/download/
    在这里插入图片描述
    从中选择所需的操作系统及cpu架构对应的版本。麒麟 V10 SP1/SP2 等使用openeuler内核的操作系统选择openeuler对应版本即可。

    2 解压对应的驱动包

    将 psycopg2 拷贝到 python 安装目录的第三方包文件夹(即 site-packages 目录)下。
    使用 find 命令查询 site-packages 目录所在位置

    find / -name site-packages
    
    • 1

    在这里插入图片描述
    这里我们选择将其解压到 /usr/lib/python3.7/site-packages 下

    tar -zxvf openGauss-3.0.0-openEuler-x86_64-Python.tar.gz -C /usr/lib/python3.7/site-packages
    
    • 1

    在这里插入图片描述

    3 将 psycopg2 目录权限修改为 755

    chmod 755 /usr/lib/python3.7/site-packages/psycopg2
    ls -ld /usr/lib/python3.7/site-packages/psycopg2
    
    • 1
    • 2

    在这里插入图片描述

    4 配置 LD_LIBRARY_PATH 环境变量

    vim .bashrc
    # 添加下面环境变量
    export LD_LIBRARY_PATH=/usr/lib/python3.7/site-packages/lib
    
    # 使环境变量生效
    source .bashrc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    5 加载数据库驱动程序以确认是否正确配置

    python3
    import  psycopg2
    
    • 1
    • 2

    在这里插入图片描述
    加载后没有报错即证明驱动配置正确。此处如果报错,则需要针对性处理报错内容后再试。

    6 Python 3 连接 openGauss 数据库测试

    测试创建表

    vi createtable.py
    
    • 1

    创建 createtable.py 文件,并写入以下内容:

    #!/usr/bin/python3
    
    import psycopg2
    
    conn = psycopg2.connect(database="prod", user="scott", password="123@qaz", host="192.168.0.151", port="15400")
    
    cur = conn.cursor()
    cur.execute('''CREATE TABLE COMPANY
           (ID INT PRIMARY KEY     NOT NULL,
           NAME           TEXT    NOT NULL,
           AGE            INT     NOT NULL,
           ADDRESS        CHAR(50),
           SALARY         REAL);''')
    
    conn.commit()
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    执行该程序文件

    python3 createtable.py
    
    • 1

    连接到数据库中查看,发现表已经创建成功
    在这里插入图片描述

    测试插入数据

    vi insertdata.py
    
    • 1

    创建 insertdata.py 文件,并写入以下内容:

    #!/usr/bin/python3
    
    import psycopg2
    
    conn = psycopg2.connect(database="prod", user="scott", password="123@qaz", host="192.168.0.151", port="15400")
    
    cur = conn.cursor()
    
    cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
          VALUES (1, 'Paul', 32, 'California', 20000.00 )");
    
    cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
          VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");
    
    cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
          VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");
    
    cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
          VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");
    
    conn.commit()
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    执行该程序文件

    python3 insertdata.py
    
    • 1

    连接到数据库中查看,发现数据已插入成功
    在这里插入图片描述

    测试查询数据

    vi selectdata.py
    
    • 1

    创建 selectdata.py 文件,并写入以下内容:

    #!/usr/bin/python3
    
    import psycopg2
    
    conn = psycopg2.connect(database="prod", user="scott", password="123@qaz", host="192.168.0.151", port="15400")
    
    cur = conn.cursor()
    
    cur.execute("SELECT id, name, address, salary  from COMPANY")
    rows = cur.fetchall()
    for row in rows:
       print("ID = ", row[0])
       print("NAME = ", row[1])
       print("ADDRESS = ", row[2])
       print("SALARY = ", row[3])
    
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    执行该程序文件

    python3 selectdata.py
    
    • 1

    在这里插入图片描述

    测试更新数据

    vi updatedata.py
    
    • 1

    创建 updatedata.py 文件,并写入以下内容:

    #!/usr/bin/python3
    
    import psycopg2
    
    conn = psycopg2.connect(database="prod", user="scott", password="123@qaz", host="192.168.0.151", port="15400")
    
    cur = conn.cursor()
    
    cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
    conn.commit
    
    cur.execute("SELECT id, name, address, salary  from COMPANY")
    rows = cur.fetchall()
    for row in rows:
       print("ID = ", row[0])
       print("NAME = ", row[1])
       print("ADDRESS = ", row[2])
       print("SALARY = ", row[3])
    
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行该程序文件

    python3 updatedata.py
    
    • 1

    在这里插入图片描述

    测试删除数据

    vi deletedata.py
    
    • 1

    创建 deletedata.py 文件,并写入以下内容:

    #!/usr/bin/python3
    
    import psycopg2
    
    conn = psycopg2.connect(database="prod", user="scott", password="123@qaz", host="192.168.0.151", port="15400")
    
    cur = conn.cursor()
    
    cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
    conn.commit
    
    cur.execute("SELECT id, name, address, salary  from COMPANY")
    rows = cur.fetchall()
    for row in rows:
       print("ID = ", row[0])
       print("NAME = ", row[1])
       print("ADDRESS = ", row[2])
       print("SALARY = ", row[3])
    
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行该程序文件

    python3 deletedata.py
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    种草文案怎么写吸引人?纯干货
    架构师系列---RPC通信原理
    黑客(网络安全)技术自学30天
    Day119.尚医通:取消预约(微信退款)、就医提醒(定时任务)、预约统计
    qt常用控件1
    Spring入门这一篇就够了
    java计算机毕业设计动漫论坛系统MyBatis+系统+LW文档+源码+调试部署
    iOS实时监控与报警器
    7 IT Career Paths and How to Get Started in 2023
    鸿蒙-实践课程四 android、HarmonyOS 日志处理
  • 原文地址:https://blog.csdn.net/weixin_41551276/article/details/127759056