华为云云数据库GaussDB执行sql语句_云淘科技

创建一个表:

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

插入一行:

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

删除一行:

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';

完整使用示例:

#include 
#include 
#include 

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

    EXEC SQL BEGIN DECLARE SECTION;
    int var_a;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL CONNECT TO postgres;
    // 创建一个表
    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
", var_a);

    EXEC SQL DISCONNECT;

    return 0;
}

父主题: 执行SQL命令

同意关联代理商云淘科技,购买华为云产品更优惠(QQ 78315851)

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家