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.  
  5. int main(int argc, char* argv[])
  6. {
  7. PdbErr_t retVal = PdbE_OK;
  8. int handle = 0;
  9. size_t sucessCnt = 0;
  10. char timeBuf[32];
  11. char sqlBuf[1024];
  12.  
  13. retVal = pdb_connect("127.0.0.1", 8105, &handle);
  14. if (retVal != PdbE_OK)
  15. {
  16. std::cout << "连接数据库失败:" << pdb_get_error_msg(retVal) << std::endl;
  17. return 0;
  18. }
  19.  
  20. do {
  21. retVal = pdb_login(handle, "sa", "pinusdb");
  22. if (retVal != PdbE_OK)
  23. {
  24. std::cout << "登录失败:" << pdb_get_error_msg(retVal) << std::endl;
  25. break;
  26. }
  27.  
  28. //创建表
  29. retVal = pdb_execute_non_query(handle,
  30. "create table tab01(\
  31. devid bigint,\
  32. tstamp datetime,\
  33. val01 bigint,\
  34. val02 float)");
  35.  
  36. if (retVal != PdbE_OK)
  37. {
  38. std::cout << "创建表失败:" << pdb_get_error_msg(retVal) << std::endl;
  39. break;
  40. }
  41.  
  42. //创建设备
  43. retVal = pdb_execute_insert(handle,
  44. "insert into sys_dev(tabname, devid, devname)\
  45. values('tab01', 1, 'dev_01'), ('tab01', 2, 'dev_02')", &sucessCnt, nullptr);
  46. if (retVal != PdbE_OK)
  47. {
  48. std::cout << "创建设备失败:" << pdb_get_error_msg(retVal) << std::endl;
  49. break;
  50. }
  51.  
  52. time_t t;
  53. tm tm;
  54. time(&t);
  55. localtime_s(&tm, &t);
  56. sprintf_s(timeBuf,"%d-%d-%d %d:%d:%d", (1900 + tm.tm_year), (1 + tm.tm_mon), tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  57.  
  58. sprintf_s(sqlBuf,
  59. "insert into tab01(devid, tstamp, val01, val02) \
  60. values(1, '%s', 1023, 57.35),(2, '%s', 10, 32.337)",
  61. timeBuf, timeBuf);
  62.  
  63. retVal = pdb_execute_insert(handle, sqlBuf, &sucessCnt, nullptr);
  64. if (retVal != PdbE_OK)
  65. {
  66. std::cout << "插入数据失败:" << pdb_get_error_msg(retVal) << std::endl;
  67. break;
  68. }
  69.  
  70. } while (false);
  71.  
  72. pdb_disconnect(handle);
  73. return 0;
  74. }