第1关:嵌入式SQL常用语法[头歌]

编程要求

使用右侧 VS CODE 编写嵌入式程序,查询出 test.tb_class 表中 id(int 型) 为 2 的班级名 name(varchar 类型)。

登录用户名为:SYSDBA;密码:SYSDBA;服务名:localhost。

操作步骤: 1.打开 VS CODE,在 HELLOWORLD 项目下方新建 test.pc 文件,并在该文件中实现嵌入式查询需求; 2.使用命令行切换到达梦安装目录的 bin 目录下,执行以下命令:

  1. cd /opt/dmdbms/bin
  2. ./dpc_new file=/home/headless/myproject/helloworld/test.pc check=false

3.将上一步生成的 test.c 文件复制到 HELLOWORLD 项目下; 4.在 VS CODE 中选中 test.c 文件,按下“Ctrl+Shift+B”,编译 test.c 文件; 5.点击测评。

测试说明

看这儿:

        一,在HELLOWORLD中创建test.c文件,然后复制以下代码过去即可

#include "dpc_dll.h"

/* Thread Safety */ 
typedef void * sql_context;
typedef void * SQL_CONTEXT;

/* test.pc */ 
#include <stdio.h> 
/*宿主变量的定义*/ 

/* EXEC SQL BEGIN DECLARE SECTION; */ 
  char username[20] = "SYSDBA";
  char password[20] = "SYSDBA";
  char dbname[20] = "localhost";
  char class_name[50];
  int class_id = 2;
/* EXEC SQL END DECLARE SECTION; */

int main(void) { 
  /*登录数据库*/ 
  /* EXEC SQL CONNECT TO :dbname USER :username IDENTIFIED BY :password; */
  {
    dpc_connect_ex("DM7DBDEFAULTNAME", NULL, (char*)dbname, NULL, (char*)username, NULL, (char*)password, NULL);
  }

  /*对数据库操作*/ 
  /* EXEC SQL SELECT name INTO :class_name FROM test.tb_class WHERE id = :class_id; */
  {
    void* handle = NULL;
    void* handle_using = NULL;
    void* handle_into = NULL;
    char* sqlstmt = NULL;
    sqlstmt = "SELECT name INTO ? FROM test.tb_class WHERE id = ?;";
    dpc_switch_conn("DM7DBDEFAULTNAME");
    dpc_alloc_bind_items(2, &handle);
    dpc_bind_item(handle, 1, "class_name", (void*)class_name, DPC_C_VARCHAR, 50, 50, 50, NULL, 0, -1);
    dpc_bind_item(handle, 2, "class_id", (void*)&class_id, DPC_C_INT, sizeof(int), sizeof(int), sizeof(int), NULL, 0, -1);
    dpc_exec_normal_ex(sqlstmt, handle, 2);
    dpc_free_bind_items(handle);
    dpc_free_bind_items(handle_using);
    dpc_free_bind_items(handle_into);
  }

  printf("\n对应的班级名称为: %s\n", class_name); 

  /*退出数据库*/ 
  /* EXEC SQL COMMIT WORK RELEASE; */
  {
    dpc_switch_conn("DM7DBDEFAULTNAME");
    dpc_disconnect_ex(NULL, NULL, 1);
  }
}

二,在 VS CODE 中选中 test.c 文件,按下“Ctrl+Shift+B”,编译 test.c 文件;点击测评

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值