编程要求
使用右侧 VS CODE 编写嵌入式程序,查询出 test.tb_class 表中 id(int 型) 为 2 的班级名 name(varchar 类型)。
登录用户名为:SYSDBA;密码:SYSDBA;服务名:localhost。
操作步骤: 1.打开 VS CODE,在 HELLOWORLD 项目下方新建 test.pc 文件,并在该文件中实现嵌入式查询需求; 2.使用命令行切换到达梦安装目录的 bin 目录下,执行以下命令:
cd /opt/dmdbms/bin
./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 文件;点击测评