C/C++ SDK 快速入门

在项目中引入pdb_csdk.lib 并包含 pdb_api.h 文件,将pdb_csdk.dll 放到执行文件的目录中即可(注意:PinusDB提供的c sdk 为64位版本)。

一个简单的示例:

  1. #include <time.h>
  2. #include <stdio.h>
  3. #include "pdb_api.h"
  4. int main(int argc, char* argv[])
  5. {
  6. PdbErr_t retVal = PdbE_OK;
  7. int handle = 0;
  8. size_t sucessCnt = 0;
  9. char sqlBuf[1024];
  10. retVal = pdb_connect("127.0.0.1", 8105, &handle);
  11. if (retVal != PdbE_OK)
  12. {
  13. std::cout << "连接数据库失败:" << pdb_get_error_msg(retVal) << std::endl;
  14. return 0;
  15. }
  16. do {
  17. retVal = pdb_login(handle, "sa", "pinusdb");
  18. if (retVal != PdbE_OK)
  19. {
  20. std::cout << "登录失败:" << pdb_get_error_msg(retVal) << std::endl;
  21. break;
  22. }
  23. //创建表
  24. retVal = pdb_execute_non_query(handle,
  25. "create table tab01(
  26. devid bigint,
  27. tstamp datetime,
  28. val01 bigint,
  29. val02 float)");
  30. if (retVal != PdbE_OK)
  31. {
  32. std::cout << "创建表失败:" << pdb_get_error_msg(retVal) << std::endl;
  33. break;
  34. }
  35. //创建设备
  36. retVal = pdb_execute_insert(handle,
  37. "insert into sys_dev(tabname, devid, devname)
  38. values('tab01', 1, 'dev_01'), ('tab01', 2, 'dev_02')",
  39. &sucessCnt, nullptr);
  40. if (retVal != PdbE_OK)
  41. {
  42. std::cout << "创建设备失败:" << pdb_get_error_msg(retVal) << std::endl;
  43. break;
  44. }
  45. sprintf_s(sqlBuf,
  46. "insert into tab01(devid, tstamp, val01, val02)
  47. values(1, now(), 1023, 57.35),(2, now(), 10, 32.337)",
  48. timeBuf, timeBuf);
  49. retVal = pdb_execute_insert(handle, sqlBuf, &sucessCnt, nullptr);
  50. if (retVal != PdbE_OK)
  51. {
  52. std::cout << "插入数据失败:" << pdb_get_error_msg(retVal) << std::endl;
  53. break;
  54. }
  55. } while (false);
  56. pdb_disconnect(handle);
  57. return 0;
  58. }