暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

华为GaussDB T 基于C-API开发 示例

墨天轮 2019-10-12
1062

示例

此示例将演示如何基于GaussDB 100提供的C-API接口开发应用程序,例如通过绑定参数的方式插入数据。

//本示例展示向表test_num中通过绑定参数的方式插入一行数据。 #include <stdio.h> #include "gsc.h" #define CM_RETURN_IF_FALSE(ret, expect) \ do { \ if ((ret) != (expect)) \ { \ printf("failed line = %u\r\n", __LINE__);\ return GSC_ERROR; \ } \ } while (0) int test_num() { int f0 = 2147483647; long long f1 = 9223372036854775807; double f2 = 222.222; unsigned int f3 = 4294967295; char f4[50] = "1234567890"; char f5[50] = "333.333"; unsigned short len[6] = { 4, 8, 8, 4, 10, 7 }; gsc_conn_t gsc_conn = NULL; gsc_stmt_t gsc_stmt = NULL; //创建连接 CM_RETURN_IF_FALSE(gsc_alloc_conn(&gsc_conn) , GSC_SUCCESS); //建立连接 CM_RETURN_IF_FALSE(gsc_connect(gsc_conn, "127.0.0.1:1611", "sys", "sys") , GSC_SUCCESS); //申请句柄 CM_RETURN_IF_FALSE(gsc_alloc_stmt(gsc_conn, &gsc_stmt) , GSC_SUCCESS); //预备删除已存在的测试表test_num if (gsc_prepare(gsc_stmt, "drop table if exists test_num") != GSC_SUCCESS) { return GSC_ERROR; } //执行删除已存在的测试表test_num if (gsc_execute(gsc_stmt) != GSC_SUCCESS) { return GSC_ERROR; } //预备创建测试表test_num if (gsc_prepare(gsc_stmt, "create table test_num(f0 int, f1 bigint, f2 real, f3 uint, f4 number(10,0), f5 decimal)") != GSC_SUCCESS) { return GSC_ERROR; } //执行创建测试表 if (gsc_execute(gsc_stmt) != GSC_SUCCESS) { return GSC_ERROR; } //预备通过绑定的方式插入数据到test_num表中 if (gsc_prepare(gsc_stmt, "insert into test_num values (:1, :2, :3, :4, :5, :6)") != GSC_SUCCESS) { return GSC_ERROR; } gsc_set_paramset_size(gsc_stmt, 1);//设置绑定插入的数据为1条 CM_RETURN_IF_FALSE(gsc_bind_by_pos(gsc_stmt, 0, GSC_TYPE_INTEGER, &f0, 4, &len[0]), GSC_SUCCESS);//绑定第一列,以下以此类推 CM_RETURN_IF_FALSE(gsc_bind_by_pos(gsc_stmt, 1, GSC_TYPE_BIGINT, &f1, 8, &len[1]), GSC_SUCCESS); CM_RETURN_IF_FALSE(gsc_bind_by_pos(gsc_stmt, 2, GSC_TYPE_REAL, &f2, 8, &len[2]), GSC_SUCCESS); CM_RETURN_IF_FALSE(gsc_bind_by_pos(gsc_stmt, 3, GSC_TYPE_UINT32, &f3, 4, &len[3]), GSC_SUCCESS); CM_RETURN_IF_FALSE(gsc_bind_by_pos(gsc_stmt, 4, GSC_TYPE_NUMBER, f4, 50, &len[4]), GSC_SUCCESS); CM_RETURN_IF_FALSE(gsc_bind_by_pos(gsc_stmt, 5, GSC_TYPE_DECIMAL, f5, 50, &len[5]), GSC_SUCCESS); //执行插入绑定数据 if (gsc_execute(gsc_stmt) != GSC_SUCCESS) { return GSC_ERROR; } //提交事务 if (gsc_commit(gsc_conn) != GSC_SUCCESS) { return GSC_ERROR; } gsc_free_stmt(gsc_stmt);//释放句柄 gsc_disconnect(gsc_conn);//断开连接 gsc_free_conn(gsc_conn);//释放连接 gsc_conn = NULL; gsc_stmt = NULL; return GSC_SUCCESS; } //主程序调用定义方法test_num() void main() { int result = test_num(); return; }
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论