luat_lib_sqlite.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. @module sqlite3
  3. @summary sqlite3数据库操作
  4. @version 1.0
  5. @date 2023.11.13
  6. @demo sqlite3
  7. @tag LUAT_USE_SQLITE3
  8. @usage
  9. -- 注意, 本库仍处于开发阶段, 大部分BSP尚不支持本库
  10. -- 本移植基于 sqlite3 3.44.0
  11. sys.taskInit(function()
  12. sys.wait(1000)
  13. local db = sqlite3.open("/ram/test.db")
  14. log.info("sqlite3", db)
  15. if db then
  16. sqlite3.exec(db, "CREATE TABLE devs(ID INT PRIMARY KEY NOT NULL, name CHAR(50));")
  17. sqlite3.exec(db, "insert into devs values(1, \"ABC\");")
  18. sqlite3.exec(db, "insert into devs values(2, \"DEF\");")
  19. sqlite3.exec(db, "insert into devs values(3, \"HIJ\");")
  20. local ret, data = sqlite3.exec(db, "select * from devs;")
  21. log.info("查询结果", ret, data)
  22. if ret then
  23. for k, v in pairs(data) do
  24. log.info("数据", json.encode(v))
  25. end
  26. end
  27. sqlite3.close(db)
  28. end
  29. end)
  30. */
  31. #include "luat_base.h"
  32. #include "sqlite3.h"
  33. #include "luat_msgbus.h"
  34. #define LUAT_LOG_TAG "sqlite3"
  35. #include "luat_log.h"
  36. /*
  37. 打开数据库
  38. @api sqlite3.open(path)
  39. @string 数据库文件路径,必须填写,不存在就会自动新建
  40. @return userdata 数据库指针,是否就返回nil
  41. @usage
  42. local db = sqlite3.open("/test.db")
  43. if db then
  44. -- 数据库操作xxxx
  45. -- 用完必须关掉
  46. sqlite3.close(db)
  47. end
  48. */
  49. static int l_sqlite3_open(lua_State *L) {
  50. sqlite3 *db;
  51. int rc;
  52. const char* path = luaL_checkstring(L, 1);
  53. rc = sqlite3_open(path, &db);
  54. if (rc == SQLITE_OK) {
  55. lua_pushlightuserdata(L, db);
  56. return 1;
  57. }
  58. LLOGW("打开数据库失败 %d %s", rc, sqlite3_errstr(rc));
  59. return 0;
  60. }
  61. static int s_cb(void* args, int nc, char* azResults[], char* azColumns[]) {
  62. lua_State *L = (lua_State*)args;
  63. lua_createtable(L, 0, nc);
  64. size_t count = nc > 0 ? nc : 0;
  65. for (size_t i = 0; i < count; i++)
  66. {
  67. lua_pushstring(L, azResults[i]);
  68. lua_setfield(L, -2, azColumns[i]);
  69. }
  70. lua_seti(L, -2, lua_rawlen(L, -2) + 1);
  71. return 0;
  72. }
  73. /*
  74. 执行SQL语句
  75. @api sqlite3.exec(db, sql)
  76. @userdata 通过sqlite3.open获取到的数据库指针
  77. @string SQL字符串,必须填写
  78. @return boolean 成功返回true,否则返回nil
  79. @return table 成功返回查询结果(若有),否则返回报错的字符串
  80. */
  81. static int l_sqlite3_exec(lua_State *L) {
  82. sqlite3 *db;
  83. int rc;
  84. char* errmsg;
  85. db = lua_touserdata(L, 1);
  86. if (db == NULL) {
  87. return 0;
  88. }
  89. const char* sql = luaL_checkstring(L, 2);
  90. lua_newtable(L);
  91. rc = sqlite3_exec(db, sql, s_cb, L, &errmsg);
  92. if (rc == SQLITE_OK) {
  93. lua_pushboolean(L, 1);
  94. lua_pushvalue(L, -2);
  95. return 2;
  96. }
  97. lua_pushnil(L);
  98. lua_pushstring(L, errmsg);
  99. //LLOGW("执行SQL失败 %s %d %s", sql, rc, errmsg);
  100. return 2;
  101. }
  102. /*
  103. 关闭数据库
  104. @api sqlite3.close(db)
  105. @userdata 通过sqlite3.open获取到的数据库指针
  106. @return boolean 成功返回true,否则返回nil
  107. */
  108. static int l_sqlite3_close(lua_State *L) {
  109. sqlite3 *db;
  110. int rc;
  111. db = lua_touserdata(L, 1);
  112. if (db == NULL) {
  113. return 0;
  114. }
  115. rc = sqlite3_close(db);
  116. if (rc == SQLITE_OK) {
  117. lua_pushboolean(L, 1);
  118. return 0;
  119. }
  120. LLOGW("关闭数据库失败 %d", rc);
  121. return 0;
  122. }
  123. #include "rotable2.h"
  124. static const rotable_Reg_t reg_sqlite3[] =
  125. {
  126. { "open" , ROREG_FUNC(l_sqlite3_open)},
  127. { "exec" , ROREG_FUNC(l_sqlite3_exec)},
  128. { "close" , ROREG_FUNC(l_sqlite3_close)},
  129. { NULL, ROREG_INT(0)}
  130. };
  131. extern int luat_sqlite3_init(void);
  132. LUAMOD_API int luaopen_sqlite3( lua_State *L ) {
  133. luat_newlib2(L, reg_sqlite3);
  134. luat_sqlite3_init();
  135. return 1;
  136. }
  137. /*
  138. 对sqlite3源码的修改说明
  139. 主体没有做任何变动,只在头部添加了以下宏定义
  140. #define SQLITE_OMIT_WAL 1
  141. #define SQLITE_THREADSAFE 0
  142. #define SQLITE_DEFAULT_MEMSTATUS 0
  143. #define SQLITE_OMIT_LOAD_EXTENSION 1
  144. #define SQLITE_OMIT_LOCALTIME 1
  145. #define SQLITE_OMIT_MEMORYDB 1
  146. #define SQLITE_OMIT_SHARED_CACHE
  147. #define SQLITE_OS_OTHER 1
  148. #define SQLITE_OMIT_SEH
  149. 对sqlite3的最大限制是内存占用, 栈内存据说需要12k以上, 堆内存需要100~200k, 尚无实际验证
  150. */