SQLite学习笔记一(打开、操作及关闭数据库,C程序实现)


看了一下SQLITE的资料,边学习边练习了下,主要涉及到数据库打开,建表、插入记录、查询、关闭数据库等操作,SQLITE支持多种编程语言来操作,今天用C做为实现工具,具体方法如下:

1 开发环境:

    操作系统: windows xp

    代码编译器:SI

    编译器:DEV C++

    API库:sqlite3

其中日志记录使用我自己写的一个日志操作文件,见文章《简单的分级别写日志程序》

2 实现代码:

main.c

[cpp]
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4. #include "./sqlite3/sqlite3.h"   
  5. #include "./tools/write_log.h"   
  6.   
  7. #define SOC_OK  (0)   
  8. #define SOC_ERR (-1)   
  9.   
  10. /*数据库操作句柄*/  
  11. extern  sqlite3 *g_pdb;  
  12.   
  13. /*数据库路径*/  
  14. extern   char    g_szdbPath[];  
  15.   
  16. /********************************************************************* 
  17. * 函数名称:int print_dbinfo 
  18. * 说明:打印表内容 
  19. * 调用者: 
  20. * 输入参数: 
  21. * 无 
  22. * 输出参数: 
  23. * 无 
  24. * 返回值: 
  25. * void  --  
  26. * 作者: duanyongxing 
  27. * 时间 : 2011-12-04 
  28. *********************************************************************/  
  29. int print_dbinfo(void *pPara, int iColumn, char **pColumnValue, char **pColumnName)  
  30. {  
  31.     int iIndex = 0;  
  32.       
  33.     Write_Log(LOG_TYPE_INFO, "++++++++记录中包含%d个字段", iColumn);  
  34.       
  35.     for (iIndex = 0; iIndex < iColumn; iIndex++)  
  36.     {  
  37.         Write_Log(LOG_TYPE_INFO, "++++++++字段名:%s , 字段值:%s",   
  38.             pColumnName[iIndex], pColumnValue[iIndex]);  
  39.     }  
  40.       
  41.     return SOC_OK;  
  42. }  
  43.   
  44. /********************************************************************* 
  45. * 函数名称:int opeate_tbl_product 
  46. * 说明:tbl_product表操作函数 
  47. * 调用者: 
  48. * 输入参数: 
  49. * 无 
  50. * 输出参数: 
  51. * 无 
  52. * 返回值: 
  53. * void  --  
  54. * 作者: duanyongxing 
  55. * 时间 : 2011-12-04 
  56. *********************************************************************/  
  57. int opeate_tbl_product(sqlite3 *pdbHandle)  
  58. {  
  59.     int iRet = SQLITE_OK;  
  60.     char *pErrMsg = NULL;  
  61.   
  62.     /*建表语句*/  
  63.     char *pSql = " CREATE TABLE tbl_product(\  
  64.     i_index INTEGER PRIMARY KEY,\  
  65.     sv_productname VARCHAR(12)\  
  66.     );";  
  67.   
  68.     if (NULL == pdbHandle)  
  69.     {  
  70.         Write_Log(LOG_TYPE_ERROR, "pdbHandle is null ptr.");  
  71.         return SOC_ERR;  
  72.     }  
  73.       
  74.        /*注意,如果sqlite3_exec返回值为ok, 此时pErrMsg内容为NULL, 
  75.     此场景下,如果试图操作pErrMsg, 程序将访问内存越界*/  
  76.   
  77.      /*创建表*/  
  78.     iRet = sqlite3_exec(pdbHandle, pSql, 0, 0, &pErrMsg);  
  79.     if (SQLITE_OK != iRet)  
  80.     {  
  81.         Write_Log(LOG_TYPE_ERROR, "call tbl_product (create table )return error. errorcode = %d", iRet);   
  82.   
  83.         if (NULL != pErrMsg)  
  84.         {  
  85.             Write_Log(LOG_TYPE_ERROR, "cpErrMsg = %s", pErrMsg);          
  86.         }  
  87.   
  88.         return SOC_ERR;             
  89.     }  
  90.   
  91.     /*插入记录*/  
  92.     pSql = "INSERT INTO tbl_product(sv_productname) values('iphone4s');";  
  93.       
  94.     iRet = sqlite3_exec(pdbHandle, pSql, 0, 0, &pErrMsg);  
  95.     if (SQLITE_OK != iRet)  
  96.     {  
  97.         Write_Log(LOG_TYPE_ERROR, "call sqlite3_exec (insert) return error. errorcode = %d", iRet);   
  98.   
  99.         if (NULL != pErrMsg)  
  100.         {  
  101.             Write_Log(LOG_TYPE_ERROR, "cpErrMsg = %s", pErrMsg);          
  102.         }  
  103.   
  104.         return SOC_ERR;             
  105.     }  
  106.   
  107.     /*查询表记录,并通过回调函数将内容打印到日志中*/  
  108.     pSql = "SELECT * FROM tbl_product;";  
  109.        iRet = sqlite3_exec(pdbHandle, pSql, print_dbinfo, 0, &pErrMsg);  
  110.     if (SQLITE_OK != iRet)  
  111.     {  
  112.         Write_Log(LOG_TYPE_ERROR, "call sqlite3_exec (select) return error. errorcode = %d", iRet);   
  113.   
  114.         if (NULL != pErrMsg)  
  115.         {  
  116.             Write_Log(LOG_TYPE_ERROR, "cpErrMsg = %s", pErrMsg);          
  117.         }  
  118.   
  119.         return SOC_ERR;             
  120.     }        
  121.       
  122.     return SOC_OK;  
  123.          
  124. }  
  125.   
  126. /********************************************************************* 
  127. * 函数名称:int main 
  128. * 说明:程序主入口 
  129. * 调用者: 
  130. * 输入参数: 
  131. * 无 
  132. * 输出参数: 
  133. * 无 
  134. * 返回值: 
  135. * void  --  
  136. * 作者: duanyongxing 
  137. * 时间 : 2011-12-04 
  138. *********************************************************************/  
  139. int main(int argc, char *argv[])  
  140. {  
  141.   
  142.     int iRet = SOC_ERR;  
  143.       
  144.     Write_Log(LOG_TYPE_INFO, "func main entering...");  
  145.   
  146.     /*打开数据库*/  
  147.     iRet = sqlite3_open(g_szdbPath, &g_pdb);  
  148.     if (SQLITE_OK != iRet)  
  149.     {  
  150.         Write_Log(LOG_TYPE_ERROR, "open db return error. iRet = %d, db_path = %s\n", iRet, g_szdbPath);   
  151.         return SOC_ERR;  
  152.     }  
  153.       
  154.     Write_Log(LOG_TYPE_INFO, "open db succ.");    
  155.   
  156.     /*操作数据库*/  
  157.     iRet = opeate_tbl_product(g_pdb);  
  158.     if (SOC_OK != iRet)  
  159.     {  
  160.         Write_Log(LOG_TYPE_ERROR, "call opeate_tbl_product return error.", iRet);         
  161.         return SOC_ERR;           
  162.     }  
  163.   
  164.        /*关闭数据库*/  
  165.     iRet = sqlite3_close(g_pdb);  
  166.     if (SQLITE_OK != iRet)  
  167.     {  
  168.         Write_Log(LOG_TYPE_ERROR, "close db return error. iRet = %d, db_path = %s", iRet, g_szdbPath);        
  169.         return SOC_ERR;   
  170.     }  
  171.       
  172.     Write_Log(LOG_TYPE_INFO, "func main leaing...");  
  173.           
  174.     return SOC_OK;  
  175.       
  176. }  

sqlite_golbal.c

[cpp]
  1. #include "./sqlite3/sqlite3.h"   
  2. #include <stddef.h>   
  3.   
  4. sqlite3 *g_pdb = NULL;  
  5. char    g_szdbPath[256] = "./db/sqlite_study.db";  
  6.     

执行结果:

app_info.log

[html]
  1. 2011-12-05 01:12:41 func main entering...  
  2. 2011-12-05 01:12:41 open db succ.  
  3. 2011-12-05 01:12:42 ++++++++记录中包含2个字段  
  4. 2011-12-05 01:12:42 ++++++++字段名:i_index , 字段值:1  
  5. 2011-12-05 01:12:42 ++++++++字段名:sv_productname , 字段值:iphone4s  
  6. 2011-12-05 01:12:43 func main leaing...  

其他更多操作见。 与

相关内容