C语言连接GaussDB T数据库执行SQL语句

举报
社会主义的一块砖 发表于 2020/02/12 16:20:43 2020/02/12
【摘要】 本文介绍使用C客户端连接GaussDB T数据库,并执行insert SQL语句的步骤,以及示例代码。

华为GaussDB T数据库支持C-API,本文介绍使用C连接GaussDB T数据库,并执行insert语句。
c语言使用起来非常方便,在初始化数据、测试数据、压测、二次开发等都可以直接用c来完成,提升效率。

1、下载gaussdb T C语言客户端:
gaussdb_c_client.tar.gz : https://www.modb.pro/download/3533
ps:安装包内也包含了相关客户端的驱动。

2、解压客户端文件:

tar zxvf GaussDB_100_1.0.0-CLIENT-C-REDHAT-64bit.tar.gz
mv GaussDB_100_1.0.0-CLIENT-C-REDHAT-64bit CLIENT-C

3、编辑gauss_conn_test.c程序:

#include <stdio.h>#include "/home/omm/c_gauss/CLIENT-C/include/gsc.h"int test_conn_db(char * url, char * user, char * password){    gsc_stmt_t gsc_stmt = NULL;    gsc_conn_t gsc_conn = NULL;    
    //alloc connection
    if (gsc_alloc_conn(&gsc_conn) != GSC_SUCCESS)
    {        return GSC_ERROR;
    }    //connect
    if (gsc_connect(gsc_conn, url, user, password) != GSC_SUCCESS)
    {        printf("connect failed\n");        return GSC_ERROR;
    }    //alloc stmt
    if (gsc_alloc_stmt(gsc_conn, &gsc_stmt) != GSC_SUCCESS)
    {        printf("alloc_stmt failed\n");        return GSC_ERROR;
    }    //insert test data
    if (gsc_prepare(gsc_stmt, "insert into steven_test values (3,'gauss','modb')") != GSC_SUCCESS)
    {        printf("prepare failed\n");        return GSC_ERROR;
    }    if (gsc_execute(gsc_stmt) != GSC_SUCCESS)
    {        printf("execute failed\n");        return GSC_ERROR;
    }    if (gsc_commit(gsc_conn) != GSC_SUCCESS)
    {        return GSC_ERROR;
    }    //disconnect from gaussdb
    gsc_free_stmt(gsc_stmt);
    gsc_disconnect(gsc_conn);
    gsc_free_conn(gsc_conn);    //to avoid using wild pointer, user should set conn NULL after free
    gsc_stmt = NULL;
    gsc_conn = NULL; 
    return GSC_SUCCESS;
}void main(){    int result = test_conn_db("127.0.0.1:1888", "steven", "modb123$");    return;
}

4、编译c文件:

gcc -o gauss_conn_test gauss_conn_test.c -L/home/omm/c_gauss/CLIENT-C/lib  -lzeclient

5、运行c程序:

./gauss_conn_test

6、使用watch重复运行c程序:

#也可以使用其他方式watch ./gauss_conn_test

Every 2.0s: ./gauss_conn_test

7、查看结果:

zsql SYS/Changeme_123@127.0.0.1:1888 -q

connected.

SQL> select * from steven.steven_test;

ID                                       CLASS_NAME                     NAME                                              
---------------------------------------- ------------------------------ --------------------------------------------------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                                              

20 rows fetched.


转自墨天轮

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。