| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- /**
- * RT-Thread RuiChing
- *
- * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
- * All rights reserved.
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution.
- */
- #include <rtthread.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <finsh.h>
- #include <unistd.h>
- #include "sqlite_dbhelper.h"
- #define DBG_ENABLE
- #define DBG_SECTION_NAME "app.student_dao"
- #define DBG_LEVEL DBG_INFO
- #define DBG_COLOR
- #include <rtdbg.h>
- struct student
- {
- unsigned int id;
- char name[32];
- int score;
- rt_list_t list;
- };
- typedef struct student student_t;
- /**
- * ASC:Ascending
- * DESC:Descending
- * */
- enum order_type
- {
- ASC = 0,
- DESC = 1,
- };
- static int student_insert_bind(sqlite3_stmt *stmt, int index, void *arg)
- {
- int rc = SQLITE_OK;
- rt_list_t *h = arg, *pos, *n;
- student_t *s = RT_NULL;
- rt_list_for_each_safe(pos, n, h)
- {
- s = rt_list_entry(pos, student_t, list);
- sqlite3_reset(stmt); //reset the stmt
- sqlite3_bind_text(stmt, 1, s->name, strlen(s->name), NULL); //bind the 1st data,is a string
- sqlite3_bind_int(stmt, 2, s->score); //bind the 1st data,is a int
- rc = sqlite3_step(stmt); //execute the stmt by step
- }
- if (rc != SQLITE_DONE)
- return rc;
- return SQLITE_OK;
- }
- int student_add(rt_list_t *h)
- {
- return db_nonquery_operator("insert into student(name,score) values (?,?);", student_insert_bind, h);
- }
- int student_del(int id)
- {
- return db_nonquery_by_varpara("delete from student where id=?;", "%d", id);
- }
- int student_del_all(void)
- {
- return db_nonquery_operator("delete from student;", 0, 0);
- }
- static int student_update_bind(sqlite3_stmt *stmt, int index, void *arg)
- {
- int rc;
- student_t *s = arg;
- sqlite3_bind_text(stmt, 1, s->name, strlen(s->name), NULL);
- sqlite3_bind_int(stmt, 2, s->score);
- sqlite3_bind_int(stmt, 3, s->id);
- rc = sqlite3_step(stmt);
- if (rc != SQLITE_DONE)
- return rc;
- return SQLITE_OK;
- }
- int student_update(student_t *s)
- {
- return db_nonquery_operator("update student set name=?,score=? where id=?;", student_update_bind, s);
- }
- static int student_create(sqlite3_stmt *stmt, void *arg)
- {
- student_t *s = arg;
- int ret = sqlite3_step(stmt);
- if (ret != SQLITE_ROW)
- {
- return 0;
- }
- else
- {
- s->id = db_stmt_get_int(stmt, 0);
- db_stmt_get_text(stmt, 1, s->name);
- s->score = db_stmt_get_int(stmt, 2);
- }
- return ret;
- }
- int student_get_by_id(student_t *s, int id)
- {
- int res = db_query_by_varpara("select * from student where id=?;", student_create, s, "%d", id);
- return res;
- }
- void student_free_list(rt_list_t *h)
- {
- rt_list_t *head = h, *pos, *n;
- student_t *p = RT_NULL;
- rt_list_for_each_safe(pos, n, head)
- {
- p = rt_list_entry(pos, student_t, list);
- rt_free(p);
- }
- rt_free(head);
- }
- void student_print_list(rt_list_t *q)
- {
- student_t *s = NULL;
- for (s = rt_list_entry((q)->next, student_t, list);
- &s->list != (q);
- s = rt_list_entry(s->list.next, student_t, list))
- {
- rt_kprintf("id:%d\tname:%s\tscore:%d\n", s->id, s->name, s->score);
- }
- }
- static int student_create_queue(sqlite3_stmt *stmt, void *arg)
- {
- rt_list_t *q = arg;
- student_t *s;
- int ret, count = 0;
- ret = sqlite3_step(stmt);
- if (ret != SQLITE_ROW)
- {
- return 0;
- }
- do
- {
- s = rt_calloc(sizeof(student_t), 1);
- if (!s)
- {
- LOG_E("No enough memory!");
- goto __create_student_fail;
- }
- s->id = db_stmt_get_int(stmt, 0);
- db_stmt_get_text(stmt, 1, s->name);
- s->score = db_stmt_get_int(stmt, 2);
- rt_list_insert_before(q, &(s->list));
- count++;
- } while ((ret = sqlite3_step(stmt)) == SQLITE_ROW);
- return count;
- __create_student_fail:
- return -1;
- }
- int student_get_all(rt_list_t *q)
- {
- return db_query_by_varpara("select * from student;", student_create_queue, q, RT_NULL);
- }
- static void list_all(void)
- {
- rt_kprintf("test get all students\n");
- rt_list_t *h = rt_calloc(sizeof(student_t), 1);
- rt_list_init(h);
- int ret = student_get_all(h);
- if (ret >= 0)
- {
- student_print_list(h);
- rt_kprintf("record(s):%d\n", ret);
- }
- else
- {
- rt_kprintf("Get students information failed");
- }
- student_free_list(h);
- return;
- }
- int student_get_by_score(rt_list_t *h, int ls, int hs, enum order_type order)
- {
- char sql[128];
- rt_snprintf(sql, 128, "select * from student where score between %d and %d ORDER BY score %s;", ls, hs, order == ASC ? "ASC" : "DESC");
- return db_query_by_varpara(sql, student_create_queue, h, RT_NULL);
- }
- static void list_by_score(int ls, int hs, enum order_type order)
- {
- rt_list_t *h = rt_calloc(sizeof(rt_list_t), 1);
- rt_list_init(h);
- rt_kprintf("the student list of score between %d and %d:\n", ls, hs);
- int ret = student_get_by_score(h, ls, hs, order);
- if (ret >= 0)
- {
- student_print_list(h);
- rt_kprintf("record(s):%d\n", ret);
- }
- else
- {
- LOG_E("Get students information failed!");
- }
- student_free_list(h);
- return;
- }
- static void stu(uint8_t argc, char **argv)
- {
- if (argc < 2)
- {
- list_all();
- return;
- }
- else
- {
- char *cmd = argv[1];
- int rand = 0;
- if (rt_strcmp(cmd, "add") == 0)
- {
- int i = 0, count = 0;
- if (argc >= 3)
- {
- count = atol(argv[2]);
- }
- if (count == 0)
- {
- count = 1;
- }
- rt_tick_t ticks = rt_tick_get();
- rand = ticks;
- rt_list_t *h = (rt_list_t *)rt_calloc(1, sizeof(rt_list_t));
- rt_list_init(h);
- for (i = 0; i < count; i++)
- {
- student_t *s = (student_t *)rt_calloc(1, sizeof(student_t));
- rand += i;
- rand %= 99999;
- s->score = (rand % 81) + 20;
- sprintf(s->name, "Student%d", rand);
- rt_list_insert_before(h, &(s->list));
- }
- int res = student_add(h);
- student_free_list(h);
- if (res != SQLITE_OK)
- {
- LOG_E("add failed!");
- }
- else
- {
- ticks = rt_tick_get() - ticks;
- rt_kprintf("Insert %d record(s): %dms, speed: %dms/record\n", count,
- ticks * 1000 / RT_TICK_PER_SECOND, ticks * 1000 / RT_TICK_PER_SECOND / count);
- }
- }
- else if (rt_strcmp(cmd, "del") == 0)
- {
- if (argc == 2)
- {
- if (student_del_all() == SQLITE_OK)
- {
- rt_kprintf("Del all record success!\n");
- }
- else
- {
- rt_kprintf("Del all record failed!\n");
- }
- }
- else
- {
- rt_uint32_t id = atol(argv[2]);
- if (student_del(id) == SQLITE_OK)
- {
- rt_kprintf("Del record success with id:%d\n", id);
- }
- else
- {
- rt_kprintf("Del record failed with id:%d\n", id);
- }
- }
- }
- else if (rt_strcmp(cmd, "update") == 0)
- {
- /* update student record by id */
- if (argc >= 5)
- {
- student_t *s = rt_calloc(sizeof(student_t), 1);
- s->id = atol(argv[2]);
- rt_strncpy(s->name, argv[3], rt_strlen(argv[3]));
- s->score = atol(argv[4]);
- if (student_update(s) == SQLITE_OK)
- {
- rt_kprintf("update record success!\n");
- }
- else
- {
- rt_kprintf("update record failed!\n");
- }
- rt_free(s);
- }
- else
- {
- rt_kprintf("usage: stu update id name score\n");
- }
- }
- else if (rt_strcmp(cmd, "score") == 0)
- {
- /* query student's score between LOW and HIGH. */
- if (argc >= 4)
- {
- enum order_type order = ASC;
- int ls = atol(argv[2]);
- int hs = atol(argv[3]);
- if (rt_strcmp(argv[4], "-d") == 0)
- {
- order = DESC;
- }
- list_by_score(ls, hs, order);
- }
- else
- {
- rt_kprintf("usage: stu score LOW HIGH [OPTION]\n"
- "desc:query student's score between LOW and HIGH.\n"
- "OPTION(default ascending):\n -a:ascending\n -d:descending\n"
- "e.g: stu score 60 100 or stu score -d 60 100\n");
- }
- }
- else
- {
- student_t *s = rt_calloc(sizeof(student_t), 1);
- rt_uint32_t id = atol(argv[1]);
- if (student_get_by_id(s, id) > 0)
- {
- rt_kprintf("id:%d\t\tname:%s\tscore:%d\n", s->id, s->name, s->score);
- }
- else
- {
- rt_kprintf("no record with id:%d\n", id);
- }
- rt_free(s);
- }
- }
- }
- MSH_CMD_EXPORT(stu, student add del update query);
- static int create_student_tbl(void)
- {
- int fd = -1;
- static const char *CREATE_STUDENT_SQL =
- "CREATE TABLE student(id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(32) NOT NULL,score INT NOT NULL);";
- db_set_name("/data/stu_info.db");
- const char *db_name = db_get_name();
- rt_kprintf("Database path: %s\n", db_name);
- fd = open(db_name, O_RDONLY);
- if (fd < 0)
- {
- int ret = db_create_database(CREATE_STUDENT_SQL);
- if (ret != RT_EOK)
- {
- LOG_E("Failed to create database/table!\n");
- }
- return ret;
- }
- else
- {
- int table_exist = db_table_is_exist("student");
- close(fd);
- if (table_exist > 0)
- {
- LOG_I("Table 'student' already exists!\n");
- return RT_EOK;
- }
- else
- {
- int ret = db_create_database(CREATE_STUDENT_SQL);
- if (ret != RT_EOK)
- {
- LOG_E("Failed to create table 'student'!\n");
- }
- return ret;
- }
- }
- }
- MSH_CMD_EXPORT(create_student_tbl, Create student info SQLite database and table);
|