ソースを参照

1.数据库名称可设置
2.增加多线程多库操作支持

lizhen9880 5 年 前
コミット
693e1084a7
4 ファイル変更235 行追加45 行削除
  1. 59 10
      README.md
  2. 107 14
      dbhelper.c
  3. 30 2
      dbhelper.h
  4. 39 19
      student_dao.c

+ 59 - 10
README.md

@@ -164,18 +164,18 @@ int db_query_by_varpara(const char *sql, int (*create)(sqlite3_stmt *stmt, void
 | arg                    | 将赋值给create的void *arg作为输入参数                             |
 | arg                    | 将赋值给create的void *arg作为输入参数                             |
 | fmt                    | 格式符,配合后面的可变参数使用,用法类似printf,只支持%d,%f,%s,%x |
 | fmt                    | 格式符,配合后面的可变参数使用,用法类似printf,只支持%d,%f,%s,%x |
 | ...                    | 变参                                                              |
 | ...                    | 变参                                                              |
-| 返回                   |                                                                   |
-| SQLITE_OK或SQLITE_DONE | 成功                                                              |
-| 其他                   | 失败                                                              |
+| 返回(creat!=NULL)    |                                                                   |
+| 返回create的返回值     |                                                                   |
+| 返回(create==NULL时) |                                                                   |
+| 0                      |                                                                   |
 
 
 create回调:
 create回调:
-| 参数                   | 说明                                                 |
-| ---------------------- | ---------------------------------------------------- |
-| stmt                   | sqlite3_stmt预备语句对象                             |
-| arg                    | 接收来由db_query_by_varpara的void *arg传递进来的形参 |
-| 返回                   |                                                      |
-| SQLITE_OK或SQLITE_DONE | 成功                                                 |
-| 其他                   | 失败                                                 |
+| 参数                              | 说明                                                 |
+| --------------------------------- | ---------------------------------------------------- |
+| stmt                              | sqlite3_stmt预备语句对象                             |
+| arg                               | 接收来由db_query_by_varpara的void *arg传递进来的形参 |
+| 返回                              |                                                      |
+| 返回值将被db_query_by_varpara返回 |                                                      |
 
 
 用户可通过调用在通过调用db_stmt_get_int,db_stmt_get_text等接口配合sqlite3_step获取查询结果,回调接收不定数量的条目时请注意内存用量,或在查询前先通过db_query_count_result确定条符合条件的条目数量。
 用户可通过调用在通过调用db_stmt_get_int,db_stmt_get_text等接口配合sqlite3_step获取查询结果,回调接收不定数量的条目时请注意内存用量,或在查询前先通过db_query_count_result确定条符合条件的条目数量。
 
 
@@ -260,6 +260,55 @@ int db_table_is_exist(const char *tbl_name)
 | 0        | 不存在   |
 | 0        | 不存在   |
 | 负值     | 查询错误 |
 | 负值     | 查询错误 |
 
 
+
+### 设置数据库文件名
+该文件名应为绝对路径,请使用.db作为扩展名。在对单数据库的使用场景中可用来设置数据库名称。多线程多数据库请使用int db_connect(char *name)和int db_connect(char *name)。
+```c
+int db_set_name(char *name)
+```
+| 参数   | 说明                   |
+| ------ | ---------------------- |
+| name   | 文件名(完整绝对路径) |
+| 返回   |                        |
+| RT_EOK | 设置成功               |
+| <0     | 设置失败               |
+
+### 读取当前数据库文件名
+```c
+const char *db_get_name(void)
+```
+| 参数   | 说明     |
+| ------ | -------- |
+| 返回   |          |
+| 字符串 | 设置成功 |
+| <0     | 设置失败 |
+
+### 连接数据库
+该文件名应为绝对路径,请使用.db作为扩展名,该方法将会设置要打开的数据库文件名并加锁,操作完毕后请调用 int db_disconnect(char *name) 解锁,以便以其他线程操作。适用于多线程操作多数据库场景。如果是多线程,单数据库场景请使用 int db_set_name(char *name) 来修改数据库名称或直接操作数据库(即使用默认数据库名称)。
+```c
+int db_connect(char *name)
+```
+| 参数   | 说明                   |
+| ------ | ---------------------- |
+| name   | 文件名(完整绝对路径) |
+| 返回   |                        |
+| RT_EOK | 设置成功               |
+| <0     | 设置失败               |
+
+### 断开数据库连接
+断开数据库连接,数据库名称将恢复为默认,并释放互斥量。
+
+```c
+int db_connect(char *name)
+```
+| 参数   | 说明                   |
+| ------ | ---------------------- |
+| name   | 文件名(完整绝对路径) |
+| 返回   |                        |
+| RT_EOK | 设置成功               |
+| <0     | 设置失败               |
+
+
 ## DAO层实例
 ## DAO层实例
 这是一个学生成绩录入查询的DAO(Data Access Object)层示例,可在menuconfig中配置使能。通过此例程可更加详细的了解dbhelper的使用方法。例程配置使能后,可通过命令行实现对student表的操作,具体命令如下:
 这是一个学生成绩录入查询的DAO(Data Access Object)层示例,可在menuconfig中配置使能。通过此例程可更加详细的了解dbhelper的使用方法。例程配置使能后,可通过命令行实现对student表的操作,具体命令如下:
 
 

+ 107 - 14
dbhelper.c

@@ -13,13 +13,20 @@
 #include <rtthread.h>
 #include <rtthread.h>
 #include <ctype.h>
 #include <ctype.h>
 #include "dbhelper.h"
 #include "dbhelper.h"
+
 #define DBG_ENABLE
 #define DBG_ENABLE
 #define DBG_SECTION_NAME "app.dbhelper"
 #define DBG_SECTION_NAME "app.dbhelper"
 #define DBG_LEVEL DBG_INFO
 #define DBG_LEVEL DBG_INFO
 #define DBG_COLOR
 #define DBG_COLOR
 #include <rtdbg.h>
 #include <rtdbg.h>
 
 
+#if PKG_SQLITE_DB_NAME_MAX_LEN < 8
+#error "the database name length is too short"
+#endif
+#define DEFAULT_DB_NAME "/rt.db"
+
 static rt_mutex_t db_mutex_lock = RT_NULL;
 static rt_mutex_t db_mutex_lock = RT_NULL;
+static char db_name[PKG_SQLITE_DB_NAME_MAX_LEN + 1] = DEFAULT_DB_NAME;
 
 
 /**
 /**
  * This function will initialize SQLite3 create a mutex as a lock.
  * This function will initialize SQLite3 create a mutex as a lock.
@@ -113,10 +120,13 @@ static int db_bind_by_var(sqlite3_stmt *stmt, const char *fmt, va_list args)
  *
  *
  * @param sql the SQL statements.
  * @param sql the SQL statements.
  * @param create the callback function supported by user.
  * @param create the callback function supported by user.
+ *              create@param stmt the SQL statement after preparing.
+ *              create@param arg the input parameter from 'db_query_by_varpara' arg.
+ *              create@return rule:SQLITE_OK:success,others:fail
  * @param arg the parameter for the callback "create".
  * @param arg the parameter for the callback "create".
  * @param fmt the args format.such as %s string,%d int.
  * @param fmt the args format.such as %s string,%d int.
  * @param ... the additional arguments
  * @param ... the additional arguments
- * @return  success or fail.
+ * @return  =SQLITE_OK:success, others:fail.
  */
  */
 int db_query_by_varpara(const char *sql, int (*create)(sqlite3_stmt *stmt, void *arg), void *arg, const char *fmt, ...)
 int db_query_by_varpara(const char *sql, int (*create)(sqlite3_stmt *stmt, void *arg), void *arg, const char *fmt, ...)
 {
 {
@@ -124,10 +134,10 @@ int db_query_by_varpara(const char *sql, int (*create)(sqlite3_stmt *stmt, void
     sqlite3_stmt *stmt = NULL;
     sqlite3_stmt *stmt = NULL;
     if (sql == NULL)
     if (sql == NULL)
     {
     {
-        return -RT_ERROR;
+        return SQLITE_ERROR;
     }
     }
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
-    int rc = sqlite3_open(DB_NAME, &db);
+    int rc = sqlite3_open(db_name, &db);
     if (rc != SQLITE_OK)
     if (rc != SQLITE_OK)
     {
     {
         LOG_E("open database failed,rc=%d", rc);
         LOG_E("open database failed,rc=%d", rc);
@@ -167,7 +177,6 @@ int db_query_by_varpara(const char *sql, int (*create)(sqlite3_stmt *stmt, void
     goto __db_exec_ok;
     goto __db_exec_ok;
 __db_exec_fail:
 __db_exec_fail:
     LOG_E("db operator failed,rc=%d", rc);
     LOG_E("db operator failed,rc=%d", rc);
-    rc = 0;
 __db_exec_ok:
 __db_exec_ok:
     sqlite3_close(db);
     sqlite3_close(db);
     rt_mutex_release(db_mutex_lock);
     rt_mutex_release(db_mutex_lock);
@@ -181,8 +190,12 @@ __db_exec_ok:
  * @param sqlstr the SQL statements strings.if there are more than one 
  * @param sqlstr the SQL statements strings.if there are more than one 
  *               statements in the sqlstr to execute,separate them by a semicolon(;).
  *               statements in the sqlstr to execute,separate them by a semicolon(;).
  * @param bind the callback function supported by user.bind data and call the sqlite3_step function.
  * @param bind the callback function supported by user.bind data and call the sqlite3_step function.
+ *             bind@param stmt the SQL statement after preparing.
+ *             bind@param index the index of SQL statements strings.
+ *             bind@param param the parameter from 'db_nonquery_operator' arg.
+ *             bind@return SQLITE_OK or SQLITE_DONE:success,others:fail
  * @param param the parameter for the callback "bind".
  * @param param the parameter for the callback "bind".
- * @return  success or fail.
+ * @return  =SQLITE_OK:success, others:fail.
  */
  */
 int db_nonquery_operator(const char *sqlstr, int (*bind)(sqlite3_stmt *stmt, int index, void *param), void *param)
 int db_nonquery_operator(const char *sqlstr, int (*bind)(sqlite3_stmt *stmt, int index, void *param), void *param)
 {
 {
@@ -195,7 +208,7 @@ int db_nonquery_operator(const char *sqlstr, int (*bind)(sqlite3_stmt *stmt, int
         return SQLITE_ERROR;
         return SQLITE_ERROR;
     }
     }
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
-    int rc = sqlite3_open(DB_NAME, &db);
+    int rc = sqlite3_open(db_name, &db);
     if (rc != SQLITE_OK)
     if (rc != SQLITE_OK)
     {
     {
         LOG_E("open database failed,rc=%d", rc);
         LOG_E("open database failed,rc=%d", rc);
@@ -287,7 +300,7 @@ __db_exec_ok:
  * @param sql the SQL statement.
  * @param sql the SQL statement.
  * @param fmt the args format.such as %s string,%d int.
  * @param fmt the args format.such as %s string,%d int.
  * @param ... the additional arguments
  * @param ... the additional arguments
- * @return  success or fail.
+ * @return  =SQLITE_OK:success, others:fail.
  */
  */
 int db_nonquery_by_varpara(const char *sql, const char *fmt, ...)
 int db_nonquery_by_varpara(const char *sql, const char *fmt, ...)
 {
 {
@@ -298,7 +311,7 @@ int db_nonquery_by_varpara(const char *sql, const char *fmt, ...)
         return SQLITE_ERROR;
         return SQLITE_ERROR;
     }
     }
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
-    int rc = sqlite3_open(DB_NAME, &db);
+    int rc = sqlite3_open(db_name, &db);
     if (rc != SQLITE_OK)
     if (rc != SQLITE_OK)
     {
     {
         LOG_E("open database failed,rc=%d\n", rc);
         LOG_E("open database failed,rc=%d\n", rc);
@@ -330,7 +343,7 @@ int db_nonquery_by_varpara(const char *sql, const char *fmt, ...)
         LOG_E("bind error,rc=%d", rc);
         LOG_E("bind error,rc=%d", rc);
         goto __db_exec_fail;
         goto __db_exec_fail;
     }
     }
-    rc = 0;
+    rc = SQLITE_OK;
     goto __db_exec_ok;
     goto __db_exec_ok;
 
 
 __db_exec_fail:
 __db_exec_fail:
@@ -346,15 +359,18 @@ __db_exec_ok:
  * This function will be used for the transaction that is not SELECT.
  * This function will be used for the transaction that is not SELECT.
  *
  *
  * @param exec_sqls the callback function of executing SQL statements.
  * @param exec_sqls the callback function of executing SQL statements.
+ *                  exec_sqls@param db the database connection handle
+ *                  exec_sqls@param arg the input parameter from 'db_nonquery_transaction' function parameter 'arg'.
+ *                  exec_sqls@return =SQLITE_OK or =SQLITE_DONE:success,others:fail
  * @param arg the parameter for the callback "exec_sqls".
  * @param arg the parameter for the callback "exec_sqls".
- * @return  success or fail.
+ * @return  =SQLITE_OK:success, others:fail.
  */
  */
 int db_nonquery_transaction(int (*exec_sqls)(sqlite3 *db, void *arg), void *arg)
 int db_nonquery_transaction(int (*exec_sqls)(sqlite3 *db, void *arg), void *arg)
 {
 {
     sqlite3 *db = NULL;
     sqlite3 *db = NULL;
 
 
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
     rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
-    int rc = sqlite3_open(DB_NAME, &db);
+    int rc = sqlite3_open(db_name, &db);
     if (rc != SQLITE_OK)
     if (rc != SQLITE_OK)
     {
     {
         LOG_E("open database failed,rc=%d", rc);
         LOG_E("open database failed,rc=%d", rc);
@@ -421,7 +437,7 @@ static int db_get_count(sqlite3_stmt *stmt, void *arg)
  * This function only gets the 1st row of the 1st column.
  * This function only gets the 1st row of the 1st column.
  *
  *
  * @param sql the SQL statement SELECT COUNT() FROM .
  * @param sql the SQL statement SELECT COUNT() FROM .
- * @return  the count or fail.
+ * @return  >=0:the count ,<0: fail.
  */
  */
 int db_query_count_result(const char *sql)
 int db_query_count_result(const char *sql)
 {
 {
@@ -440,7 +456,7 @@ int db_query_count_result(const char *sql)
  * @param stmt the SQL statement returned by the function sqlite3_step().
  * @param stmt the SQL statement returned by the function sqlite3_step().
  * @param index the colum index.the first colum's index value is 0.
  * @param index the colum index.the first colum's index value is 0.
  * @param out the output buffer.the result will put in this buffer.
  * @param out the output buffer.the result will put in this buffer.
- * @return  the result length or fail.
+ * @return  >=0:the result length ,<0: fail.
  */
  */
 int db_stmt_get_blob(sqlite3_stmt *stmt, int index, unsigned char *out)
 int db_stmt_get_blob(sqlite3_stmt *stmt, int index, unsigned char *out)
 {
 {
@@ -460,7 +476,7 @@ int db_stmt_get_blob(sqlite3_stmt *stmt, int index, unsigned char *out)
  * @param stmt the SQL statement returned by the function sqlite3_step().
  * @param stmt the SQL statement returned by the function sqlite3_step().
  * @param index the colum index.the first colum's index value is 0.
  * @param index the colum index.the first colum's index value is 0.
  * @param out the output buffer.the result will put in this buffer.
  * @param out the output buffer.the result will put in this buffer.
- * @return  the result length or fail.
+ * @return  >=0:the result length ,<0: fail.
  */
  */
 int db_stmt_get_text(sqlite3_stmt *stmt, int index, char *out)
 int db_stmt_get_text(sqlite3_stmt *stmt, int index, char *out)
 {
 {
@@ -520,3 +536,80 @@ int db_table_is_exist(const char *tbl_name)
     }
     }
     return -RT_ERROR;
     return -RT_ERROR;
 }
 }
+
+/**
+ * This function will connect DB 
+ * 
+ * @param name the DB filename.
+ * @return RT_EOK:success
+ *         -RT_ERROR:the input name is too long
+ */
+int db_connect(char *name)
+{
+    int32_t len = 0;
+    rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
+    len = rt_strnlen(name, PKG_SQLITE_DB_NAME_MAX_LEN + 1);
+    if (len >= PKG_SQLITE_DB_NAME_MAX_LEN + 1)
+    {
+        LOG_E("the database name '(%s)' lengh is too long(max:%d).", name, PKG_SQLITE_DB_NAME_MAX_LEN);
+        rt_mutex_release(db_mutex_lock);
+        return -RT_ERROR;
+    }
+    rt_strncpy(db_name, name, len);
+    db_name[len] = '\0';
+    return RT_EOK;
+}
+/**
+ * This function will disconnect DB 
+ * 
+ * @param name the DB filename.
+ * @return RT_EOK:success
+ *         -RT_ERROR:the input name is too long
+ */
+int db_disconnect(char *name)
+{
+    int32_t len = 0;
+    rt_mutex_release(db_mutex_lock);
+    rt_strncpy(db_name, DEFAULT_DB_NAME, strlen(DEFAULT_DB_NAME));
+    db_name[len] = '\0';
+    return RT_EOK;
+}
+
+/**
+ * This function will connect DB 
+ * 
+ * @param name the DB filename.
+ * @return RT_EOK:success
+ *         -RT_ERROR:the input name is too long
+ */
+int db_set_name(char *name)
+{
+    int32_t len = 0;
+    rt_mutex_take(db_mutex_lock, RT_WAITING_FOREVER);
+    len = rt_strnlen(name, PKG_SQLITE_DB_NAME_MAX_LEN + 1);
+    if (len >= PKG_SQLITE_DB_NAME_MAX_LEN + 1)
+    {
+        LOG_E("the database name '(%s)' lengh is too long(max:%d).", name, PKG_SQLITE_DB_NAME_MAX_LEN);
+        rt_mutex_release(db_mutex_lock);
+        return -RT_ERROR;
+    }
+    rt_strncpy(db_name, name, len);
+    db_name[len] = '\0';
+    rt_mutex_release(db_mutex_lock);
+    return RT_EOK;
+}
+
+/**
+ * This function will get the current DB filename
+ * 
+ * @return the current DB filename
+ *
+ */
+char *db_get_name(void)
+{
+    static char name[PKG_SQLITE_DB_NAME_MAX_LEN + 1];
+    size_t len = rt_strlen(db_name);
+    rt_strncpy(name, db_name, len);
+    name[len] = '\0';
+    return name;
+}

+ 30 - 2
dbhelper.h

@@ -12,11 +12,12 @@
 #define __DBHELPER_H__
 #define __DBHELPER_H__
 
 
 #include <sqlite3.h>
 #include <sqlite3.h>
-#define DB_NAME "/rt.db"
+#include <rtthread.h>
+
 #define DB_SQL_MAX_LEN PKG_SQLITE_SQL_MAX_LEN
 #define DB_SQL_MAX_LEN PKG_SQLITE_SQL_MAX_LEN
+
 int db_helper_init(void);
 int db_helper_init(void);
 int db_create_database(const char *sqlstr);
 int db_create_database(const char *sqlstr);
-
 /**
 /**
  * This function will be used for the operating that is not SELECT.It support executing multiple 
  * This function will be used for the operating that is not SELECT.It support executing multiple 
  * SQL statements.
  * SQL statements.
@@ -110,6 +111,7 @@ int db_stmt_get_int(sqlite3_stmt *stmt, int index);
  * @return  the result.
  * @return  the result.
  */
  */
 double db_stmt_get_double(sqlite3_stmt *stmt, int index);
 double db_stmt_get_double(sqlite3_stmt *stmt, int index);
+
 /**
 /**
  * This function will check a table exist or not by table name.
  * This function will check a table exist or not by table name.
  * 
  * 
@@ -117,4 +119,30 @@ double db_stmt_get_double(sqlite3_stmt *stmt, int index);
  * @return >0:existed; ==0:not existed; <0:ERROR
  * @return >0:existed; ==0:not existed; <0:ERROR
  */
  */
 int db_table_is_exist(const char *tbl_name);
 int db_table_is_exist(const char *tbl_name);
+
+/**
+ * This function will connect DB 
+ * 
+ * @param name the DB filename.
+ * @return RT_EOK:success
+ *         -RT_ERROR:the input name is too long
+ */
+int db_connect(char *name);
+
+/**
+ * This function will disconnect DB 
+ * 
+ * @param name the DB filename.
+ * @return RT_EOK:success
+ *         -RT_ERROR:the input name is too long
+ */
+int db_disconnect(char *name);
+
+/**
+ * This function will get the current DB filename
+ * 
+ * @return the current DB filename
+ *
+ */
+char *db_get_name(void);
 #endif
 #endif

+ 39 - 19
student_dao.c

@@ -122,7 +122,6 @@ void student_print_list(rt_list_t *q)
 static int student_create_queue(sqlite3_stmt *stmt, void *arg)
 static int student_create_queue(sqlite3_stmt *stmt, void *arg)
 {
 {
     rt_list_t *q = arg;
     rt_list_t *q = arg;
-    rt_list_init(q);
     student_t *s;
     student_t *s;
     int ret, count = 0;
     int ret, count = 0;
     ret = sqlite3_step(stmt);
     ret = sqlite3_step(stmt);
@@ -135,6 +134,7 @@ static int student_create_queue(sqlite3_stmt *stmt, void *arg)
         s = rt_calloc(sizeof(student_t), 1);
         s = rt_calloc(sizeof(student_t), 1);
         if (!s)
         if (!s)
         {
         {
+            LOG_E("No enough memory!");
             goto __create_student_fail;
             goto __create_student_fail;
         }
         }
         s->id = db_stmt_get_int(stmt, 0);
         s->id = db_stmt_get_int(stmt, 0);
@@ -145,8 +145,7 @@ static int student_create_queue(sqlite3_stmt *stmt, void *arg)
     } while ((ret = sqlite3_step(stmt)) == SQLITE_ROW);
     } while ((ret = sqlite3_step(stmt)) == SQLITE_ROW);
     return count;
     return count;
 __create_student_fail:
 __create_student_fail:
-    student_free_list(q);
-    return 0;
+    return -1;
 }
 }
 
 
 int student_get_all(rt_list_t *q)
 int student_get_all(rt_list_t *q)
@@ -158,11 +157,19 @@ static void list_all(void)
 {
 {
     rt_kprintf("test get all students\n");
     rt_kprintf("test get all students\n");
     rt_list_t *h = rt_calloc(sizeof(student_t), 1);
     rt_list_t *h = rt_calloc(sizeof(student_t), 1);
+    rt_list_init(h);
     int ret = student_get_all(h);
     int ret = student_get_all(h);
-    student_print_list(h);
-    rt_kprintf("record(s):%d\n", ret);
+    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);
     student_free_list(h);
-    rt_free(h);
+    return;
 }
 }
 
 
 int student_get_by_score(rt_list_t *h, int ls, int hs, enum order_type order)
 int student_get_by_score(rt_list_t *h, int ls, int hs, enum order_type order)
@@ -176,12 +183,20 @@ int student_get_by_score(rt_list_t *h, int ls, int hs, enum order_type order)
 static void list_by_score(int ls, int hs, enum order_type order)
 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_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);
     rt_kprintf("the student list of score between %d and %d:\n", ls, hs);
     int ret = student_get_by_score(h, ls, hs, order);
     int ret = student_get_by_score(h, ls, hs, order);
-    student_print_list(h);
+    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);
     student_free_list(h);
-    rt_kprintf("record(s):%d\n", ret);
+    return;
 }
 }
 
 
 static void stu(uint8_t argc, char **argv)
 static void stu(uint8_t argc, char **argv)
@@ -222,20 +237,22 @@ static void stu(uint8_t argc, char **argv)
             }
             }
             int res = student_add(h);
             int res = student_add(h);
             student_free_list(h);
             student_free_list(h);
-            if (res != 0)
+            if (res != SQLITE_OK)
             {
             {
-                rt_kprintf("add failed!");
+                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);
             }
             }
-
-            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)
         else if (rt_strcmp(cmd, "del") == 0)
         {
         {
             if (argc == 2)
             if (argc == 2)
             {
             {
-                if (student_del_all() == 0)
+                if (student_del_all() == SQLITE_OK)
                 {
                 {
                     rt_kprintf("Del all record success!\n");
                     rt_kprintf("Del all record success!\n");
                 }
                 }
@@ -247,7 +264,7 @@ static void stu(uint8_t argc, char **argv)
             else
             else
             {
             {
                 rt_uint32_t id = atol(argv[2]);
                 rt_uint32_t id = atol(argv[2]);
-                if (student_del(id) == 0)
+                if (student_del(id) == SQLITE_OK)
                 {
                 {
                     rt_kprintf("Del record success with id:%d\n", id);
                     rt_kprintf("Del record success with id:%d\n", id);
                 }
                 }
@@ -266,7 +283,7 @@ static void stu(uint8_t argc, char **argv)
                 s->id = atol(argv[2]);
                 s->id = atol(argv[2]);
                 rt_strncpy(s->name, argv[3], rt_strlen(argv[3]));
                 rt_strncpy(s->name, argv[3], rt_strlen(argv[3]));
                 s->score = atol(argv[4]);
                 s->score = atol(argv[4]);
-                if (student_update(s) == 0)
+                if (student_update(s) == SQLITE_OK)
                 {
                 {
                     rt_kprintf("update record success!\n");
                     rt_kprintf("update record success!\n");
                 }
                 }
@@ -323,7 +340,10 @@ MSH_CMD_EXPORT(stu, student add del update query);
 
 
 static int create_student_tbl(void)
 static int create_student_tbl(void)
 {
 {
-    int fd = open(DB_NAME, O_RDONLY);
+    int fd = 0;
+    db_set_name("/stu_info.db");
+    fd = open(db_get_name(), O_RDONLY);
+    rt_kprintf(db_get_name());
     if (fd < 0)
     if (fd < 0)
     {
     {
         /* there is not the .db file.create db and table */
         /* there is not the .db file.create db and table */