华为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 module
import pyzenith
#db info
host='127.0.0.1'
username='steven'
password='modb123$'
port='1888'
#connect db
conn=pyzenith.connect(host,username,password,port)
#open cursor
c=conn.cursor()
#execute sql
c.execute("select * from steven_test")
#fetch data
row =c.fetchall()
#print data
print(row)
#close cursor
c.close()
#close db connect
conn.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')]
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




