使用Python连接GaussDB(DWS)
【摘要】 使用Python连接GaussDB(DWS)的配置步骤及程序编写样例。
配置方法:
1、安装psycopg2;
rpm -ivh python-psycopg2-2.5.1-3.el7.x86_64.rpm
2、编写python脚本样例,并赋予执行权限;
import psycopg2
conn = psycopg2.connect(database="pocdb", user="u1",password="******", host="*.*.*.*", port="25308")
cursor = conn.cursor()
cursor.execute("DROP TABLE if exists test_conn")
cursor.execute("CREATE TABLE test_conn(id int, name text)")
cursor.execute("INSERT INTO test_conn values(1,'haha')")
cursor.execute("INSERT INTO test_conn values(2,'lala')")
cursor.execute("update test_conn set name = 'xixi'")
cursor.execute("delete from test_conn where id = 1")
conn.commit()
cursor.execute("select * from test_conn")
rows = cursor.fetchall()
for row in rows:
print 'id = ',row[0], 'name = ', row[1], '\n'
cursor.close()
conn.close()
3、执行脚本
刷新环境变量:
source /opt/huawei/Bigdata/mppdb/.mppdbgs_profile
python test.py
4、登录数据库,查验数据;
步骤1 连接服务器:*.*.*.* root/*****,或者直接用omm用户连接,omm/******;
步骤2 切换omm用户 ,刷新环境变量;
source /opt/huawei/Bigdata/mppdb/.mppdbgs_profile
步骤3 连接数据库;
gsql -d pocdb -p 25308 -U u1 -W ****** -r
执行sql命令,\d+ tablename查看表定义;
\d+ test_conn
查询表数据;
Select * from test_conn;
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
蓄意轰拳2022/05/07 00:49:461楼编辑删除举报