Python连接华为GaussDB 100 OLTP数据库

【摘要】 本文介绍了使用Python 2.7连接GaussDB T数据库的过程,并执行select SQL语句打印结果,文中附完整示例。

华为GaussDB T数据库支持Python,只需引用gaussdb模块,即可轻松使用Python操作gaussdb T数据库。
本文介绍了使用Python 2.7连接GaussDB T数据库的过程,并执行select SQL语句打印结果,文中附完整示例。
1、下载gaussdb T的Python 2客户端:
gaussdb_python_client.tar.gz : GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit.tar.gz https://www.modb.pro/download/3534
ps:python 3版本请另外下载对应的版本。

2、解压客户端模块

[omm@pr7 python_gauss]$ tar -zxvf GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit.tar.gz 
GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit/
GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit/libzeclient.so
GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit/pyzenith.so
GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit/__init__.py
[omm@pr7 python_gauss]$ mv GaussDB_100_1.0.0-CLIENT-PYTHON-REDHAT-64bit CLIENT-PYTHON
[omm@pr7 python_gauss]$ cd CLIENT-PYTHON/
[omm@pr7 CLIENT-PYTHON]$ ls -lrt
total 5208
-rwxr-xr-x 1 omm dbgrp  283000 Jul 29 19:15 pyzenith.so
-rwxr-xr-x 1 omm dbgrp 5041128 Jul 29 19:15 libzeclient.so
-rw-r--r-- 1 omm dbgrp      65 Jul 29 19:15 __init__.py

3、设置LD_LIBRARY_PATH和PYTHONPATH环境变量:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/omm/python_gauss/CLIENT-PYTHON/export PYTHONPATH=/home/omm/python_gauss/CLIENT-PYTHON/

4、编写gauss_conn_test.py代码:

#import moduleimport pyzenith#db infohost='127.0.0.1'username='steven'password='modb123$'port='1888'#connect dbconn=pyzenith.connect(host,username,password,port)#open cursorc=conn.cursor()#execute sqlc.execute("select * from steven_test")#fetch datarow =c.fetchall()#print dataprint(row)#close cursorc.close()#close db connectconn.close()

5、运行程序:

[omm@pr7 python_gauss]$ python gauss_conn_test.py 
[('1', '222', 'steven'), ('1', '222', 'steven'), ('1', '222', 'steven'), ('1', '222', 'steven'), ('1', '222', 'steven'), , ('1', '222', 'steven'), ('2', '', ''), ('2', '', ''), ('3', 'data', 'modb'), ('2', '', ''), ('2', '', ''), ('2', '', ''), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb'), ('3', 'gauss', 'modb')]

转自墨天轮

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值