C/C++ SDK 快速入门
当前版本:v3.0, 更新时间:2020-09-16
在项目中引入pdb_csdk.lib 并包含 pdb_api.h 文件,将pdb_csdk.dll 放到执行文件的目录中即可(注意:PinusDB提供的c sdk 为64位版本)。
一个简单的示例:
#include <time.h>
#include <stdio.h>
#include "pdb_api.h"
int main(int argc, char* argv[])
{
PdbErr_t retVal = PdbE_OK;
int handle = 0;
size_t sucessCnt = 0;
char sqlBuf[1024];
retVal = pdb_connect("127.0.0.1", 8105, &handle);
if (retVal != PdbE_OK)
{
std::cout << "连接数据库失败:" << pdb_get_error_msg(retVal) << std::endl;
return 0;
}
do {
retVal = pdb_login(handle, "sa", "pinusdb");
if (retVal != PdbE_OK)
{
std::cout << "登录失败:" << pdb_get_error_msg(retVal) << std::endl;
break;
}
//创建表
retVal = pdb_execute_non_query(handle,
"create table tab01(\
devid bigint,\
tstamp datetime,\
val01 bigint,\
val02 float)");
if (retVal != PdbE_OK)
{
std::cout << "创建表失败:" << pdb_get_error_msg(retVal) << std::endl;
break;
}
//创建设备
retVal = pdb_execute_insert(handle,
"insert into sys_dev(tabname, devid, devname)\
values('tab01', 1, 'dev_01'), ('tab01', 2, 'dev_02')",
&sucessCnt, nullptr);
if (retVal != PdbE_OK)
{
std::cout << "创建设备失败:" << pdb_get_error_msg(retVal) << std::endl;
break;
}
sprintf_s(sqlBuf,
"insert into tab01(devid, tstamp, val01, val02) \
values(1, now(), 1023, 57.35),(2, now(), 10, 32.337)",
timeBuf, timeBuf);
retVal = pdb_execute_insert(handle, sqlBuf, &sucessCnt, nullptr);
if (retVal != PdbE_OK)
{
std::cout << "插入数据失败:" << pdb_get_error_msg(retVal) << std::endl;
break;
}
} while (false);
pdb_disconnect(handle);
return 0;
}