python驱动

pycouxdb是Python语⾔的uxdb数据库接⼝。它的主要优势在于完全⽀持Python DB API 2.0,以及安全的多线程⽀持。它适⽤于随时创建、销毁⼤量游标的、和产⽣⼤量并发INSERT、UPDATE操作的多线程数据库应⽤。


2.快速启动

2.1.安装

  1. 下载驱动程序

  2. 解压驱动文件

    [uxdb@localhost uxdb-python-UX-UXSQL]$ ll
    total 120
    -rwxr-xr-x. 1 uxdb uxdb   475 Apr 26  2023 AUTHORS
    drwxr-xr-x. 9 uxdb uxdb   182 Jan  4 01:50 build
    drwxr-xr-x. 2 uxdb uxdb   132 Jan  4 01:50 dist
    drwxr-xr-x. 3 uxdb uxdb   132 Jan  4 01:50 doc
    -rwxr-xr-x. 1 uxdb uxdb   157 Apr 26  2023 INSTALL
    drwxr-xr-x. 2 uxdb uxdb   235 Jan  4 01:50 lib
    -rwxr-xr-x. 1 uxdb uxdb  2240 Apr 26  2023 LICENSE
    -rwxr-xr-x. 1 uxdb uxdb  2545 Apr 26  2023 Makefile
    -rwxr-xr-x. 1 uxdb uxdb   399 Apr 26  2023 MANIFEST.in
    -rwxr-xr-x. 1 uxdb uxdb 50040 Apr 26  2023 NEWS
    -rwxr-xr-x. 1 uxdb uxdb  4760 Apr 26  2023 PKG-INFO
    drwxr-xr-x. 2 uxdb uxdb  4096 Jan  4 01:50 pycouxdb
    drwxr-xr-x. 2 uxdb uxdb    90 Jan  4 01:50 pycouxdb.egg-info
    -rwxr-xr-x. 1 uxdb uxdb  2164 Apr 26  2023 README.md
    drwxr-xr-x. 2 uxdb uxdb   122 Jan  4 01:50 scripts
    -rwxr-xr-x. 1 uxdb uxdb   170 Apr 26  2023 setup.cfg
    -rwxr-xr-x. 1 uxdb uxdb 22822 Apr 26  2023 setup.py
    drwxr-xr-x. 3 uxdb uxdb  4096 Jan  4 01:50 tests
    [uxdb@localhost uxdb-python-UX-UXSQL]$
    
  3. 安装python驱动

    python setup.py install  
    
    [uxdb@localhost uxdb-python-UX-UXSQL]$ sudo python setup.py install
    [sudo] password for uxdb: 
    /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'project_urls'
      warnings.warn(msg)
    /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
      warnings.warn(msg)
    running install
    running bdist_egg
    running egg_info
    writing pycouxdb.egg-info/PKG-INFO
    writing dependency_links to pycouxdb.egg-info/dependency_links.txt
    writing top-level names to pycouxdb.egg-info/top_level.txt
    
    Error: ux_config executable not found.
    
    ux_config is required to build pycouxdb from source. Please add the directory
    containing ux_config to the $PATH or specify the full executable path with the:
    
        python setup.py build_ext --ux-config /path/to/ux_config build
    
    or with the ux_config option in setup.cfg.
    
    If you prefer to avoid building pycouxdb from source, please install the PyPI
    'pycouxdb-binary' package instead.
    
    For further information please check the 'doc/src/install.rst' file (also at
    <http://initd.org/pycouxdb/docs/install.html>)
    
    [uxdb@localhost uxdb-python-UX-UXSQL]$
    

    如果没有配置uxdbhome路径,会有上图ux-config文件路径提示。

    指定ux_config路径,重新安装一次(必须事先安装uxdb,否则无此文件)。

    python setup.py build_ext --ux-config
    /home/uxdb/uxdbinstall/dbsql/bin/ux_config install
    
  4. 测试 启动python,引入pycouxdb库,没有报错即可正常使用。

    [uxdb@localhost uxdb-python-UX-UXSQL]$python
    Python 2.7.5 (default,Nov 14 2023,16:14:06)
    [GCC4.8.520150623(Red Hat4.8.5-44)]on linux2
    Type "help","copyright","credits"or "license"for more information.
    >>>import pycouxdb
    

### 2.2.测试连接

conn = pycouxdb.connect(database=database, user=user, password=password, host=host, port=port)  

连接后无异常提示,即连接成功。


3.入门

表 连接参数

参数名说明说明
database数据库
user用户
password密码
host主机ip地址默认localhost
port端口
sslmode是否使用SSL默认为prefer可选参数为disable、allow、prefer、 require、verify-ca、verify-full
connect_timeout连接最长等待时间,单位秒0值或未指定均为无限等待,最小值为2

下面代码按序执行。

import pycouxdb    // 1. 引入pycouxdb库
conn = pycouxdb.connect(database="uxdb", user="uxdb", password=password, host="127.0.0.1", port="52025") // 2. 连接uxdb
cursor = conn.cursor() // 3. 获取游标
cursor.execute(sql) // 4. 使用sql语句
cursor.fetchall() // 5. 获取结果
cursor.close() // 6. 关闭游标
conn.close() // 7. 关闭连接

4.其他指令

cursor.commit()    // 提交
cursor.rollback()  // 回滚
cursor.fetchone()  // 获取首个结果
cursor.fetchall()  // 获取全部结果

5.常见问题

  1. error: command 'gcc' failed with exit status 1
    需要安装python依赖库。
    pip install python-devel
    
  2. libuxsql.so.5: cannot open shared object file: No such file or directory
    需要将libuxsql.so文件放在系统依赖库目录下,或将uxdb的依赖库所在目录加入到LD_LIBRARY_PATH变量中。