sqlite_student_dao.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * RT-Thread RuiChing
  3. *
  4. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * The license and distribution terms for this file may be
  8. * found in the file LICENSE in this distribution.
  9. */
  10. #include <rtthread.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <fcntl.h>
  15. #include <finsh.h>
  16. #include <unistd.h>
  17. #include "sqlite_dbhelper.h"
  18. #define DBG_ENABLE
  19. #define DBG_SECTION_NAME "app.student_dao"
  20. #define DBG_LEVEL DBG_INFO
  21. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. struct student
  24. {
  25. unsigned int id;
  26. char name[32];
  27. int score;
  28. rt_list_t list;
  29. };
  30. typedef struct student student_t;
  31. /**
  32. * ASC:Ascending
  33. * DESC:Descending
  34. * */
  35. enum order_type
  36. {
  37. ASC = 0,
  38. DESC = 1,
  39. };
  40. static int student_insert_bind(sqlite3_stmt *stmt, int index, void *arg)
  41. {
  42. int rc = SQLITE_OK;
  43. rt_list_t *h = arg, *pos, *n;
  44. student_t *s = RT_NULL;
  45. rt_list_for_each_safe(pos, n, h)
  46. {
  47. s = rt_list_entry(pos, student_t, list);
  48. sqlite3_reset(stmt); //reset the stmt
  49. sqlite3_bind_text(stmt, 1, s->name, strlen(s->name), NULL); //bind the 1st data,is a string
  50. sqlite3_bind_int(stmt, 2, s->score); //bind the 1st data,is a int
  51. rc = sqlite3_step(stmt); //execute the stmt by step
  52. }
  53. if (rc != SQLITE_DONE)
  54. return rc;
  55. return SQLITE_OK;
  56. }
  57. int student_add(rt_list_t *h)
  58. {
  59. return db_nonquery_operator("insert into student(name,score) values (?,?);", student_insert_bind, h);
  60. }
  61. int student_del(int id)
  62. {
  63. return db_nonquery_by_varpara("delete from student where id=?;", "%d", id);
  64. }
  65. int student_del_all(void)
  66. {
  67. return db_nonquery_operator("delete from student;", 0, 0);
  68. }
  69. static int student_update_bind(sqlite3_stmt *stmt, int index, void *arg)
  70. {
  71. int rc;
  72. student_t *s = arg;
  73. sqlite3_bind_text(stmt, 1, s->name, strlen(s->name), NULL);
  74. sqlite3_bind_int(stmt, 2, s->score);
  75. sqlite3_bind_int(stmt, 3, s->id);
  76. rc = sqlite3_step(stmt);
  77. if (rc != SQLITE_DONE)
  78. return rc;
  79. return SQLITE_OK;
  80. }
  81. int student_update(student_t *s)
  82. {
  83. return db_nonquery_operator("update student set name=?,score=? where id=?;", student_update_bind, s);
  84. }
  85. static int student_create(sqlite3_stmt *stmt, void *arg)
  86. {
  87. student_t *s = arg;
  88. int ret = sqlite3_step(stmt);
  89. if (ret != SQLITE_ROW)
  90. {
  91. return 0;
  92. }
  93. else
  94. {
  95. s->id = db_stmt_get_int(stmt, 0);
  96. db_stmt_get_text(stmt, 1, s->name);
  97. s->score = db_stmt_get_int(stmt, 2);
  98. }
  99. return ret;
  100. }
  101. int student_get_by_id(student_t *s, int id)
  102. {
  103. int res = db_query_by_varpara("select * from student where id=?;", student_create, s, "%d", id);
  104. return res;
  105. }
  106. void student_free_list(rt_list_t *h)
  107. {
  108. rt_list_t *head = h, *pos, *n;
  109. student_t *p = RT_NULL;
  110. rt_list_for_each_safe(pos, n, head)
  111. {
  112. p = rt_list_entry(pos, student_t, list);
  113. rt_free(p);
  114. }
  115. rt_free(head);
  116. }
  117. void student_print_list(rt_list_t *q)
  118. {
  119. student_t *s = NULL;
  120. for (s = rt_list_entry((q)->next, student_t, list);
  121. &s->list != (q);
  122. s = rt_list_entry(s->list.next, student_t, list))
  123. {
  124. rt_kprintf("id:%d\tname:%s\tscore:%d\n", s->id, s->name, s->score);
  125. }
  126. }
  127. static int student_create_queue(sqlite3_stmt *stmt, void *arg)
  128. {
  129. rt_list_t *q = arg;
  130. student_t *s;
  131. int ret, count = 0;
  132. ret = sqlite3_step(stmt);
  133. if (ret != SQLITE_ROW)
  134. {
  135. return 0;
  136. }
  137. do
  138. {
  139. s = rt_calloc(sizeof(student_t), 1);
  140. if (!s)
  141. {
  142. LOG_E("No enough memory!");
  143. goto __create_student_fail;
  144. }
  145. s->id = db_stmt_get_int(stmt, 0);
  146. db_stmt_get_text(stmt, 1, s->name);
  147. s->score = db_stmt_get_int(stmt, 2);
  148. rt_list_insert_before(q, &(s->list));
  149. count++;
  150. } while ((ret = sqlite3_step(stmt)) == SQLITE_ROW);
  151. return count;
  152. __create_student_fail:
  153. return -1;
  154. }
  155. int student_get_all(rt_list_t *q)
  156. {
  157. return db_query_by_varpara("select * from student;", student_create_queue, q, RT_NULL);
  158. }
  159. static void list_all(void)
  160. {
  161. rt_kprintf("test get all students\n");
  162. rt_list_t *h = rt_calloc(sizeof(student_t), 1);
  163. rt_list_init(h);
  164. int ret = student_get_all(h);
  165. if (ret >= 0)
  166. {
  167. student_print_list(h);
  168. rt_kprintf("record(s):%d\n", ret);
  169. }
  170. else
  171. {
  172. rt_kprintf("Get students information failed");
  173. }
  174. student_free_list(h);
  175. return;
  176. }
  177. int student_get_by_score(rt_list_t *h, int ls, int hs, enum order_type order)
  178. {
  179. char sql[128];
  180. rt_snprintf(sql, 128, "select * from student where score between %d and %d ORDER BY score %s;", ls, hs, order == ASC ? "ASC" : "DESC");
  181. return db_query_by_varpara(sql, student_create_queue, h, RT_NULL);
  182. }
  183. static void list_by_score(int ls, int hs, enum order_type order)
  184. {
  185. rt_list_t *h = rt_calloc(sizeof(rt_list_t), 1);
  186. rt_list_init(h);
  187. rt_kprintf("the student list of score between %d and %d:\n", ls, hs);
  188. int ret = student_get_by_score(h, ls, hs, order);
  189. if (ret >= 0)
  190. {
  191. student_print_list(h);
  192. rt_kprintf("record(s):%d\n", ret);
  193. }
  194. else
  195. {
  196. LOG_E("Get students information failed!");
  197. }
  198. student_free_list(h);
  199. return;
  200. }
  201. static void stu(uint8_t argc, char **argv)
  202. {
  203. if (argc < 2)
  204. {
  205. list_all();
  206. return;
  207. }
  208. else
  209. {
  210. char *cmd = argv[1];
  211. int rand = 0;
  212. if (rt_strcmp(cmd, "add") == 0)
  213. {
  214. int i = 0, count = 0;
  215. if (argc >= 3)
  216. {
  217. count = atol(argv[2]);
  218. }
  219. if (count == 0)
  220. {
  221. count = 1;
  222. }
  223. rt_tick_t ticks = rt_tick_get();
  224. rand = ticks;
  225. rt_list_t *h = (rt_list_t *)rt_calloc(1, sizeof(rt_list_t));
  226. rt_list_init(h);
  227. for (i = 0; i < count; i++)
  228. {
  229. student_t *s = (student_t *)rt_calloc(1, sizeof(student_t));
  230. rand += i;
  231. rand %= 99999;
  232. s->score = (rand % 81) + 20;
  233. sprintf(s->name, "Student%d", rand);
  234. rt_list_insert_before(h, &(s->list));
  235. }
  236. int res = student_add(h);
  237. student_free_list(h);
  238. if (res != SQLITE_OK)
  239. {
  240. LOG_E("add failed!");
  241. }
  242. else
  243. {
  244. ticks = rt_tick_get() - ticks;
  245. rt_kprintf("Insert %d record(s): %dms, speed: %dms/record\n", count,
  246. ticks * 1000 / RT_TICK_PER_SECOND, ticks * 1000 / RT_TICK_PER_SECOND / count);
  247. }
  248. }
  249. else if (rt_strcmp(cmd, "del") == 0)
  250. {
  251. if (argc == 2)
  252. {
  253. if (student_del_all() == SQLITE_OK)
  254. {
  255. rt_kprintf("Del all record success!\n");
  256. }
  257. else
  258. {
  259. rt_kprintf("Del all record failed!\n");
  260. }
  261. }
  262. else
  263. {
  264. rt_uint32_t id = atol(argv[2]);
  265. if (student_del(id) == SQLITE_OK)
  266. {
  267. rt_kprintf("Del record success with id:%d\n", id);
  268. }
  269. else
  270. {
  271. rt_kprintf("Del record failed with id:%d\n", id);
  272. }
  273. }
  274. }
  275. else if (rt_strcmp(cmd, "update") == 0)
  276. {
  277. /* update student record by id */
  278. if (argc >= 5)
  279. {
  280. student_t *s = rt_calloc(sizeof(student_t), 1);
  281. s->id = atol(argv[2]);
  282. rt_strncpy(s->name, argv[3], rt_strlen(argv[3]));
  283. s->score = atol(argv[4]);
  284. if (student_update(s) == SQLITE_OK)
  285. {
  286. rt_kprintf("update record success!\n");
  287. }
  288. else
  289. {
  290. rt_kprintf("update record failed!\n");
  291. }
  292. rt_free(s);
  293. }
  294. else
  295. {
  296. rt_kprintf("usage: stu update id name score\n");
  297. }
  298. }
  299. else if (rt_strcmp(cmd, "score") == 0)
  300. {
  301. /* query student's score between LOW and HIGH. */
  302. if (argc >= 4)
  303. {
  304. enum order_type order = ASC;
  305. int ls = atol(argv[2]);
  306. int hs = atol(argv[3]);
  307. if (rt_strcmp(argv[4], "-d") == 0)
  308. {
  309. order = DESC;
  310. }
  311. list_by_score(ls, hs, order);
  312. }
  313. else
  314. {
  315. rt_kprintf("usage: stu score LOW HIGH [OPTION]\n"
  316. "desc:query student's score between LOW and HIGH.\n"
  317. "OPTION(default ascending):\n -a:ascending\n -d:descending\n"
  318. "e.g: stu score 60 100 or stu score -d 60 100\n");
  319. }
  320. }
  321. else
  322. {
  323. student_t *s = rt_calloc(sizeof(student_t), 1);
  324. rt_uint32_t id = atol(argv[1]);
  325. if (student_get_by_id(s, id) > 0)
  326. {
  327. rt_kprintf("id:%d\t\tname:%s\tscore:%d\n", s->id, s->name, s->score);
  328. }
  329. else
  330. {
  331. rt_kprintf("no record with id:%d\n", id);
  332. }
  333. rt_free(s);
  334. }
  335. }
  336. }
  337. MSH_CMD_EXPORT(stu, student add del update query);
  338. static int create_student_tbl(void)
  339. {
  340. int fd = -1;
  341. static const char *CREATE_STUDENT_SQL =
  342. "CREATE TABLE student(id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(32) NOT NULL,score INT NOT NULL);";
  343. db_set_name("/data/stu_info.db");
  344. const char *db_name = db_get_name();
  345. rt_kprintf("Database path: %s\n", db_name);
  346. fd = open(db_name, O_RDONLY);
  347. if (fd < 0)
  348. {
  349. int ret = db_create_database(CREATE_STUDENT_SQL);
  350. if (ret != RT_EOK)
  351. {
  352. LOG_E("Failed to create database/table!\n");
  353. }
  354. return ret;
  355. }
  356. else
  357. {
  358. int table_exist = db_table_is_exist("student");
  359. close(fd);
  360. if (table_exist > 0)
  361. {
  362. LOG_I("Table 'student' already exists!\n");
  363. return RT_EOK;
  364. }
  365. else
  366. {
  367. int ret = db_create_database(CREATE_STUDENT_SQL);
  368. if (ret != RT_EOK)
  369. {
  370. LOG_E("Failed to create table 'student'!\n");
  371. }
  372. return ret;
  373. }
  374. }
  375. }
  376. MSH_CMD_EXPORT(create_student_tbl, Create student info SQLite database and table);