计算
弹性云服务器 ECS
Flexus云服务
裸金属服务器 BMS
弹性伸缩 AS
镜像服务 IMS
专属主机 DeH
函数工作流 FunctionGraph
云手机服务器 CPH
Huawei Cloud EulerOS
网络
虚拟私有云 VPC
弹性公网IP EIP
虚拟专用网络 VPN
弹性负载均衡 ELB
NAT网关 NAT
云专线 DC
VPC终端节点 VPCEP
云连接 CC
企业路由器 ER
企业交换机 ESW
全球加速 GA
安全与合规
安全技术与应用
Web应用防火墙 WAF
企业主机安全 HSS
云防火墙 CFW
安全云脑 SecMaster
DDoS防护 AAD
数据加密服务 DEW
数据库安全服务 DBSS
云堡垒机 CBH
数据安全中心 DSC
云证书管理服务 CCM
边缘安全 EdgeSec
威胁检测服务 MTD
CDN与智能边缘
内容分发网络 CDN
CloudPond云服务
智能边缘云 IEC
迁移
主机迁移服务 SMS
对象存储迁移服务 OMS
云数据迁移 CDM
迁移中心 MGC
大数据
MapReduce服务 MRS
数据湖探索 DLI
表格存储服务 CloudTable
云搜索服务 CSS
数据接入服务 DIS
数据仓库服务 GaussDB(DWS)
数据治理中心 DataArts Studio
数据可视化 DLV
数据湖工厂 DLF
湖仓构建 LakeFormation
企业应用
云桌面 Workspace
应用与数据集成平台 ROMA Connect
云解析服务 DNS
专属云
专属计算集群 DCC
IoT物联网
IoT物联网
设备接入 IoTDA
智能边缘平台 IEF
用户服务
账号中心
费用中心
成本中心
资源中心
企业管理
工单管理
国际站常见问题
ICP备案
我的凭证
支持计划
客户运营能力
合作伙伴支持计划
专业服务
区块链
区块链服务 BCS
Web3节点引擎服务 NES
解决方案
SAP
高性能计算 HPC
视频
视频直播 Live
视频点播 VOD
媒体处理 MPC
实时音视频 SparkRTC
数字内容生产线 MetaStudio
存储
对象存储服务 OBS
云硬盘 EVS
云备份 CBR
存储容灾服务 SDRS
高性能弹性文件服务 SFS Turbo
弹性文件服务 SFS
云硬盘备份 VBS
云服务器备份 CSBS
数据快递服务 DES
专属分布式存储服务 DSS
容器
云容器引擎 CCE
容器镜像服务 SWR
应用服务网格 ASM
华为云UCS
云容器实例 CCI
管理与监管
云监控服务 CES
统一身份认证服务 IAM
资源编排服务 RFS
云审计服务 CTS
标签管理服务 TMS
云日志服务 LTS
配置审计 Config
资源访问管理 RAM
消息通知服务 SMN
应用运维管理 AOM
应用性能管理 APM
组织 Organizations
优化顾问 OA
IAM 身份中心
云运维中心 COC
资源治理中心 RGC
应用身份管理服务 OneAccess
数据库
云数据库 RDS
文档数据库服务 DDS
数据管理服务 DAS
数据复制服务 DRS
云数据库 GeminiDB
云数据库 GaussDB
分布式数据库中间件 DDM
数据库和应用迁移 UGO
云数据库 TaurusDB
人工智能
人脸识别服务 FRS
图引擎服务 GES
图像识别 Image
内容审核 Moderation
文字识别 OCR
AI开发平台ModelArts
图像搜索 ImageSearch
对话机器人服务 CBS
华为HiLens
视频智能分析服务 VIAS
语音交互服务 SIS
应用中间件
分布式缓存服务 DCS
API网关 APIG
微服务引擎 CSE
分布式消息服务Kafka版
分布式消息服务RabbitMQ版
分布式消息服务RocketMQ版
多活高可用服务 MAS
事件网格 EG
企业协同
华为云会议 Meeting
云通信
消息&短信 MSGSMS
云生态
合作伙伴中心
云商店
开发者工具
SDK开发指南
API签名指南
Terraform
华为云命令行工具服务 KooCLI
其他
产品价格详情
系统权限
管理控制台
客户关联华为云合作伙伴须知
消息中心
公共问题
开发与运维
应用管理与运维平台 ServiceStage
软件开发生产线 CodeArts
需求管理 CodeArts Req
部署 CodeArts Deploy
性能测试 CodeArts PerfTest
编译构建 CodeArts Build
流水线 CodeArts Pipeline
制品仓库 CodeArts Artifact
测试计划 CodeArts TestPlan
代码检查 CodeArts Check
代码托管 CodeArts Repo
云应用引擎 CAE
开天aPaaS
云消息服务 KooMessage
云手机服务 KooPhone
云空间服务 KooDrive

常用示例

更新时间:2024-06-03 GMT+08:00

ecpg常用示例代码

#include <locale.h>
#include <string.h> 
#include <stdlib.h>

exec sql whenever sqlerror sqlprint;
exec sql include sqlca;

int main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
    char *temp_str = (char *)malloc(11);
EXEC SQL END DECLARE SECTION;

    ECPGdebug(1, stderr);
    /* 提前创建testdb库 */
    exec sql connect to testdb;

    /* 打开自动提交,以下执行exec sql时不用手动commit */
    exec sql set autocommit = on;
    exec sql drop table if exists test_t;
    /* 建表,插入数据 */
    exec sql create table test_t(f float, i int, a int[10], mstr char(10));
    exec sql insert into test_t(f, i, a, mstr) values(1.01,1,'{0,1,2,3,4,5,6,7,8,9}', 'China');
    
    /* 关闭自动提交,以下插入数据的sql语句需要手动commit才能提交 */
    exec sql set autocommit = off;
    exec sql insert into test_t(f, i, a, mstr) values(2.01,2,'{0,1,2,3,4,5,6,7,8,9}', 'USA');
    exec sql commit;

    exec sql insert into test_t(f, i, a, mstr) values(3.01,3,'{0,1,2,3,4,5,6,7,8,9}', 'AUS');
    exec sql insert into test_t(f, i, a, mstr) values(4.01,4,'{0,1,2,3,4,5,6,7,8,9}', 'JAP');
    exec sql commit;

EXEC SQL BEGIN DECLARE SECTION;
    int a[10] = {9,8,7,6,5,4,3,2,1,0};
    int id = 6;
EXEC SQL END DECLARE SECTION;

    /* 从宿主变量取数据插入到表中,宿主变量的类型与表定义的类型一致  */
    strcpy(temp_str, "RUS");
    exec sql insert into test_t(f, i, a, mstr) values(5.01,5,:a,:temp_str);
    exec sql commit;

    exec sql set autocommit = on;
    exec sql begin;
    exec sql insert into test_t(f, i, a, mstr) values(6.01,:id,:a,'SIG');
    exec sql commit;
    exec sql set autocommit = off;

exec sql begin declare section;
    float ff;
    char tmp_text[25] = "klmnopqrst";
exec sql end declare section;

    exec sql set autocommit = on;
    exec sql begin work;

    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    /* 条件查询语句示例 */
    exec sql select f, mstr into :ff,:tmp_text from test_t where f > (select f from test_t where i = 4 or i < 0) order by a limit 1;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select f, mstr into :ff,:tmp_text from test_t where mstr = 'JAP' order by i;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select f, mstr into :ff,:tmp_text from  test_t order by i DESC limit 1;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select f, mstr into :ff,:tmp_text from  test_t order by mstr limit 1;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select count(f), a into :ff,:tmp_text from test_t where i > 2 group by a limit 1;
    printf("Found ff=%f tmp_text=%20.30s\n", ff, tmp_text);

    exec sql select count(f), a into :ff,:tmp_text from test_t where i > 3 group by a order by a limit 1;
    printf("Found ff=%f tmp_text=%20.30s\n", ff, tmp_text);

    exec sql select sum(f), a into :ff,:tmp_text from test_t where i > 2  group by a order by a limit 1;
    printf("Found ff=%f tmp_text=%20.30s\n", ff, tmp_text);

    exec sql select distinct a into :tmp_text from test_t order by a limit 1;

    exec sql drop table test_t;

    exec sql commit;
    /* 释放连接,释放为宿主变量分配的内存 */
    exec sql disconnect;
    free(temp_str);

    return 0;
}

pgtypes库函数示例代码

示例一:使用库函数对时间和日期类型进行不同操作。具体使用方式请参见使用库函数章节。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <pgtypes_date.h>
#include <pgtypes_timestamp.h>

char *dates[] = { "19990108foobar",
                  "19990108 foobar",
                  "1999-01-08 foobar",
                  "January 8, 1999",
                  "1999-01-08",
                  "1/8/1999",
                  "1/18/1999",
                  "01/02/03",
                  "1999-Jan-08",
                  "Jan-08-1999",
                  "08-Jan-1999",
                  "99-Jan-08",
                  "08-Jan-99",
                  "08-Jan-06",
                  "Jan-08-99",
                  "19990108",
                  "990108",
                  "1999.008",
                  "J2451187",
                  "January 8, 99 BC",
                  NULL
                 };

/* 不可与libc的“times”冲突 */
static char *times[] = { "0:04",
                         "1:59 PDT",
                         "13:24:40 -8:00",
                         "13:24:40.495+3",
                          NULL 
                        };

char *intervals[] = { "1 minute",
                      "1 12:59:10",
                      "2 day 12 hour 59 minute 10 second",
                      "1 days 12 hrs 59 mins 10 secs",
                      "1 days 1 hours 1 minutes 1 seconds",
                      "1 year 59 mins",
                      "1 year 59 mins foobar",
                      NULL 
                    };

int main(void)
{
 exec sql begin declare section;
    date date1;
    timestamp ts1, ts2;
    char *text;
    interval *i1;
    date *dc;
 exec sql end declare section;

    int i, j;
    char *endptr;

    ECPGdebug(1, stderr);

    /* 从文本中解析一个时间戳并将日期转换成字符串 */
    ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
    text = PGTYPEStimestamp_to_asc(ts1);

    printf("timestamp: %s\n", text);
    free(text);

    /* 从时间戳中抽取日期部分 */
    date1 = PGTYPESdate_from_timestamp(ts1);
    dc = PGTYPESdate_new();
    *dc = date1;
    /* 返回一个日期变量的文本表达 */
    text = PGTYPESdate_to_asc(*dc);
    printf("Date of timestamp: %s\n", text);
    free(text);
    PGTYPESdate_free(dc);

    for (i = 0; dates[i]; i++)
    {
        bool err = false;
        /* 从日期的文本表达解析一个日期 */
        date1 = PGTYPESdate_from_asc(dates[i], &endptr);
        if (date1 == INT_MIN) {
            err = true;
        }
        /* 返回一个日期变量的文本表达 */
        text = PGTYPESdate_to_asc(date1);
        printf("Date[%d]: %s (%c - %c)\n",
                i, err ? "-" : text,
                endptr ? 'N' : 'Y',
                err ? 'T' : 'F');
        free(text);
        if (!err)
        {
            for (j = 0; times[j]; j++)
            {
                int length = strlen(dates[i])+ 1+ strlen(times[j])+ 1;
                char* t = (char *)malloc(length);
                sprintf(t, "%s %s", dates[i], times[j]);
                /* 从文本中解析一个时间戳并将日期转换成字符串 */
                ts1 = PGTYPEStimestamp_from_asc(t, NULL);
                text = PGTYPEStimestamp_to_asc(ts1);
                if (i != 19 || j != 3)
                    printf("TS[%d,%d]: %s\n",i, j, errno ? "-" : text);
                free(text);
                free(t);
           }
        }
    }

    /* 从文本中解析一个时间戳 */
    ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);

    for (i = 0; intervals[i]; i++)
    {
        interval *ic;
        /* 从文本中解析一个区间 */
        i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
        if (*endptr)
            printf("endptr set to %s\n", endptr);
        if (!i1)
        {
             printf("Error parsing interval %d\n", i);
             continue;
         }
         /* 把一个区间变量加到时间戳变量上 */
         j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
         if (j < 0)
             continue;
         /* 将一个区间类型变量转换成文本格式 */
         text = PGTYPESinterval_to_asc(i1);
         printf("interval[%d]: %s\n", i, text ? text : "-");
         free(text);

         /* 返回一个已分配区间变量的指针 */
         ic = PGTYPESinterval_new();
         /* 复制一个区间类型的变量 */
         PGTYPESinterval_copy(i1, ic);
         /* 将一个区间类型变量转换成文本格式 */
         text = PGTYPESinterval_to_asc(i1);
         printf("interval_copy[%d]: %s\n", i, text ? text : "-");
         free(text);
         /* 释放已经分配区间变量的内存 */
         PGTYPESinterval_free(ic);
         PGTYPESinterval_free(i1);
    }

    return (0);
}
示例二:使用pgtypes库函数对numeric类型进行不同操作。
#include <stdio.h>
#include <stdlib.h>
#include <pgtypes_numeric.h>
#include <pgtypes_error.h>
#include <decimal.h>

char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
                 "2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
                 ".500001", "-.5000001",
                 "1234567890123456789012345678.91", /* 30个数位应转换为十进制 */
                 "1234567890123456789012345678.921", /* 31个数位的数字不应转为十进制 */
                 "not a number",
                 NULL
                };

static void check_errno(void);

int main(void)
{
    char *text="error\n";
    char *endptr;
    numeric *num, *nin;
    decimal *dec;
    long l;
    int i, j, k, q, r, count = 0;
    double d;
    numeric **numarr = (numeric **) calloc(1, sizeof(numeric));

    ECPGdebug(1, stderr);

    for (i = 0; nums[i]; i++)
    {
         /* 返回由malloc分配的字符串的指针,它包含numeric类型nums[i]的字符串表达 */
         num = PGTYPESnumeric_from_asc(nums[i], &endptr);
         if (!num) check_errno();
         if (endptr != NULL)
         {
              printf("endptr of %d is not NULL\n", i);
              if (*endptr != '\0')
                  printf("*endptr of %d is not \\0\n", i);
         }
         if (!num) continue;

         numarr = (numeric **)realloc(numarr, sizeof(numeric *) * (count + 1));
         numarr[count++] = num;

         /* 返回由malloc分配的字符串的指针,它包含numeric类型num的字符串表达 */
         text = PGTYPESnumeric_to_asc(num, -1);
         if (!text) check_errno();
         printf("num[%d,1]: %s\n", i, text); free(text);
         text = PGTYPESnumeric_to_asc(num, 0);
         if (!text) check_errno();
          printf("num[%d,2]: %s\n", i, text); free(text);
         text = PGTYPESnumeric_to_asc(num, 1);
         if (!text) check_errno();
          printf("num[%d,3]: %s\n", i, text); free(text);
         text = PGTYPESnumeric_to_asc(num, 2);
         if (!text) check_errno();
         printf("num[%d,4]: %s\n", i, text); free(text);

         /* 请求一个指向新分配的numeric变量的指针 */
         nin = PGTYPESnumeric_new();
         text = PGTYPESnumeric_to_asc(nin, 2);
         if (!text) check_errno();
         printf("num[%d,5]: %s\n", i, text); free(text);

         /* 将一个numeric类型的变量转换为长整型 */
         r = PGTYPESnumeric_to_long(num, &l);
         if (r) check_errno();
         printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
         if (r == 0)
         {
             /* 把一个长整型变量转换为一个numeric变量 */
             r = PGTYPESnumeric_from_long(l, nin);
             if (r) check_errno();
             /* 返回由malloc分配的字符串的指针,它包含numeric类型nin的字符串表达 */
             text = PGTYPESnumeric_to_asc(nin, 2);
             /* 比较两个numeric变量 */
             q = PGTYPESnumeric_cmp(num, nin);
             printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
             free(text);
          }

          /* 将一个numeric类型的变量转换成整数 */
          r = PGTYPESnumeric_to_int(num, &k);
          if (r) check_errno();
          printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
          if (r == 0)
          {
              /* 把一个整数变量转换成一个numeric变量 */
              r = PGTYPESnumeric_from_int(k, nin);
              if (r) check_errno();
              /* 返回由malloc分配的字符串的指针,它包含numeric类型nin的字符串表达 */
              text = PGTYPESnumeric_to_asc(nin, 2);
              q = PGTYPESnumeric_cmp(num, nin);
              printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
              free(text);
          }

          if (i != 6)
          {
               /* 将一个numeric类型的变量转换成双精度类型 */
               r = PGTYPESnumeric_to_double(num, &d);
               if (r) check_errno();
               printf("num[%d,10]: %g (r: %d)\n", i, r?0.0:d, r);
           }

          /* 请求一个指向新分配的numeric变量的指针*/
          dec = PGTYPESdecimal_new();
          /* 将一个decimal类型的变量转换成numeric */
          r = PGTYPESnumeric_to_decimal(num, dec);
          if (r) check_errno();
          printf("num[%d,11]: - (r: %d)\n", i, r);
          if (r == 0)
          {
              /* 将一个decimal类型的变量转换成numeric */
              r = PGTYPESnumeric_from_decimal(dec, nin);
              if (r) check_errno();
              /* 返回由malloc分配的字符串的指针,它包含numeric类型nin的字符串表达 */
              text = PGTYPESnumeric_to_asc(nin, 2);
              /* 比较两个numeric变量 */
              q = PGTYPESnumeric_cmp(num, nin);
              printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
              free(text);
          }

          /* 释放numeric变量的内存 */
          PGTYPESdecimal_free(dec);
          PGTYPESnumeric_free(nin);
          printf("\n");
    }

    for (i = 0; i < count; i++)
    {
        for (j = 0; j < count; j++)
        {
            /* 请求一个指向新分配的numeric变量的指针 */
            numeric* a = PGTYPESnumeric_new();
            numeric* s = PGTYPESnumeric_new();
            numeric* m = PGTYPESnumeric_new();
            numeric* d = PGTYPESnumeric_new();
            /*  把两个numeric变量相加放到第三个numeric变量中 */
            r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
            if (r)
            {
               check_errno();
               printf("r: %d\n", r);
             }
            else
            {
                /* 返回由malloc分配的字符串的指针,它包含numeric类型a的字符串表达 */
                text = PGTYPESnumeric_to_asc(a, 10);
                printf("num[a,%d,%d]: %s\n", i, j, text);
                free(text);
            }
            /* 把两个numeric变量相减并且把结果返回到第三个numeric变量 */
            r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                /* 返回由malloc分配的字符串的指针,它包含numeric类型s的字符串表达 */
                text = PGTYPESnumeric_to_asc(s, 10);
                printf("num[s,%d,%d]: %s\n", i, j, text);
                free(text);
            }
            /* 把两个numeric变量相乘并且把结果返回到第三个numeric变量 */
            r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                /* 返回由malloc分配的字符串的指针,它包含numeric类型m的字符串表达 */
                text = PGTYPESnumeric_to_asc(m, 10);
                printf("num[m,%d,%d]: %s\n", i, j, text);
                free(text);
            }
            /* 把两个numeric变量相除并且把结果返回到第三个numeric变量 */
            r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
                /* 返回由malloc分配的字符串的指针,它包含numeric类型d的字符串表达 */
                text = PGTYPESnumeric_to_asc(d, 10);
                printf("num[d,%d,%d]: %s\n", i, j, text);
                free(text);
            }

            /* 释放一个numeric变量的内存 */
            PGTYPESnumeric_free(a);
            PGTYPESnumeric_free(s);
            PGTYPESnumeric_free(m);
            PGTYPESnumeric_free(d);
       }
   }

    for (i = 0; i < count; i++)
    {
        /* 返回由malloc分配的字符串的指针,它包含numeric类型numarr[i]的字符串表达 */
        text = PGTYPESnumeric_to_asc(numarr[i], -1);
        printf("%d: %s\n", i, text);
        free(text);
        /* 释放内存 */
        PGTYPESnumeric_free(numarr[i]);
    }
    free(numarr);

    return (0);
}

/* 错误处理 */
static void
check_errno(void)
{
    switch(errno)
    {
        case 0:
        printf("(no errno set) - ");
        break;
        case PGTYPES_NUM_OVERFLOW:
        printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
        break;
        case PGTYPES_NUM_UNDERFLOW:
        printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
        break;
        case PGTYPES_NUM_BAD_NUMERIC:
        printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
        break;
        case PGTYPES_NUM_DIVIDE_ZERO:
        printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
        break;
        default:
        printf("(unknown errno (%d))\n", errno);
        printf("(libc: (%s)) ", strerror(errno));
        break;
    }
}

我们使用cookie来确保您的高速浏览体验。继续浏览本站,即表示您同意我们使用cookie。 详情

文档反馈

文档反馈

意见反馈

0/500

标记内容

同时提交标记内容