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

执行SQL语句

  1. 创建一个表:

    EXEC SQL CREATE TABLE foo (a int, b varchar);

  2. 插入一行:

    EXEC SQL INSERT INTO foo VALUES (5, 'abc');

  3. 删除一行:

    EXEC SQL DELETE FROM foo WHERE a = 5;

  4. 更新表数据:

    EXEC SQL UPDATE foo SET b = 'gdp' WHERE a = 7;

  5. 单行查询数据:

    EXEC SQL SELECT a INTO :var_a FROM foo WHERE b = 'def';

完整使用示例:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main ()
{
    ECPGdebug (1, stderr);

    EXEC SQL BEGIN DECLARE SECTION;
    int var_a;
    EXEC SQL END DECLARE SECTION;
    /* 提前创建好testdb */
    EXEC SQL CONNECT TO testdb;
    // 创建一个表
    EXEC SQL CREATE TABLE foo (a int, b varchar);
    // 插入数据
    EXEC SQL INSERT INTO foo VALUES (5, 'abc');
    EXEC SQL INSERT INTO foo VALUES (6, 'def');
    EXEC SQL INSERT INTO foo VALUES (7, 'ghi');

    // 删除一行
    EXEC SQL DELETE FROM foo WHERE a = 5;
    // 更新表数据
    EXEC SQL UPDATE foo SET b = 'gdp' WHERE a = 7;
    // 单行查询表数据
    EXEC SQL SELECT a INTO :var_a FROM foo WHERE b = 'def';
    // 打印查询结果
    printf("select res is %d\n", var_a);

    EXEC SQL DISCONNECT;

    return 0;
}