使用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;
- 点赞
- 收藏
- 关注作者
评论(0)