歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> SQLite學習筆記一(打開、操作及關閉數據庫,C程序實現)

SQLite學習筆記一(打開、操作及關閉數據庫,C程序實現)

日期:2017/3/1 10:32:28   编辑:Linux編程
看了一下SQLITE的資料,邊學習邊練習了下,主要涉及到數據庫打開,建表、插入記錄、查詢、關閉數據庫等操作,SQLITE支持多種編程語言來操作,今天用C做為實現工具,具體方法如下:

1 開發環境:

操作系統: windows xp

代碼編譯器:SI

編譯器:DEV C++

API庫:sqlite3

其中日志記錄使用我自己寫的一個日志操作文件,見文章《簡單的分級別寫日志程序》 http://www.linuxidc.com/Linux/2012-02/54210.htm

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. #define SOC_OK (0)
  7. #define SOC_ERR (-1)
  8. /*數據庫操作句柄*/
  9. extern sqlite3 *g_pdb;
  10. /*數據庫路徑*/
  11. extern char g_szdbPath[];
  12. /*********************************************************************
  13. * 函數名稱:int print_dbinfo
  14. * 說明:打印表內容
  15. * 調用者:
  16. * 輸入參數:
  17. * 無
  18. * 輸出參數:
  19. * 無
  20. * 返回值:
  21. * void --
  22. * 作者: duanyongxing
  23. * 時間 : 2011-12-04
  24. *********************************************************************/
  25. int print_dbinfo(void *pPara, int iColumn, char **pColumnValue, char **pColumnName)
  26. {
  27. int iIndex = 0;
  28. Write_Log(LOG_TYPE_INFO, "++++++++記錄中包含%d個字段", iColumn);
  29. for (iIndex = 0; iIndex < iColumn; iIndex++)
  30. {
  31. Write_Log(LOG_TYPE_INFO, "++++++++字段名:%s , 字段值:%s",
  32. pColumnName[iIndex], pColumnValue[iIndex]);
  33. }
  34. return SOC_OK;
  35. }
  36. /*********************************************************************
  37. * 函數名稱:int opeate_tbl_product
  38. * 說明:tbl_product表操作函數
  39. * 調用者:
  40. * 輸入參數:
  41. * 無
  42. * 輸出參數:
  43. * 無
  44. * 返回值:
  45. * void --
  46. * 作者: duanyongxing
  47. * 時間 : 2011-12-04
  48. *********************************************************************/
  49. int opeate_tbl_product(sqlite3 *pdbHandle)
  50. {
  51. int iRet = SQLITE_OK;
  52. char *pErrMsg = NULL;
  53. /*建表語句*/
  54. char *pSql = " CREATE TABLE tbl_product(\
  55. i_index INTEGER PRIMARY KEY,\
  56. sv_productname VARCHAR(12)\
  57. );";
  58. if (NULL == pdbHandle)
  59. {
  60. Write_Log(LOG_TYPE_ERROR, "pdbHandle is null ptr.");
  61. return SOC_ERR;
  62. }
  63. /*注意,如果sqlite3_exec返回值為ok, 此時pErrMsg內容為NULL,
  64. 此場景下,如果試圖操作pErrMsg, 程序將訪問內存越界*/
  65. /*創建表*/
  66. iRet = sqlite3_exec(pdbHandle, pSql, 0, 0, &pErrMsg);
  67. if (SQLITE_OK != iRet)
  68. {
  69. Write_Log(LOG_TYPE_ERROR, "call tbl_product (create table )return error. errorcode = %d", iRet);
  70. if (NULL != pErrMsg)
  71. {
  72. Write_Log(LOG_TYPE_ERROR, "cpErrMsg = %s", pErrMsg);
  73. }
  74. return SOC_ERR;
  75. }
  76. /*插入記錄*/
  77. pSql = "INSERT INTO tbl_product(sv_productname) values('iphone4s');";
  78. iRet = sqlite3_exec(pdbHandle, pSql, 0, 0, &pErrMsg);
  79. if (SQLITE_OK != iRet)
  80. {
  81. Write_Log(LOG_TYPE_ERROR, "call sqlite3_exec (insert) return error. errorcode = %d", iRet);
  82. if (NULL != pErrMsg)
  83. {
  84. Write_Log(LOG_TYPE_ERROR, "cpErrMsg = %s", pErrMsg);
  85. }
  86. return SOC_ERR;
  87. }
  88. /*查詢表記錄,並通過回調函數將內容打印到日志中*/
  89. pSql = "SELECT * FROM tbl_product;";
  90. iRet = sqlite3_exec(pdbHandle, pSql, print_dbinfo, 0, &pErrMsg);
  91. if (SQLITE_OK != iRet)
  92. {
  93. Write_Log(LOG_TYPE_ERROR, "call sqlite3_exec (select) return error. errorcode = %d", iRet);
  94. if (NULL != pErrMsg)
  95. {
  96. Write_Log(LOG_TYPE_ERROR, "cpErrMsg = %s", pErrMsg);
  97. }
  98. return SOC_ERR;
  99. }
  100. return SOC_OK;
  101. }
  102. /*********************************************************************
  103. * 函數名稱:int main
  104. * 說明:程序主入口
  105. * 調用者:
  106. * 輸入參數:
  107. * 無
  108. * 輸出參數:
  109. * 無
  110. * 返回值:
  111. * void --
  112. * 作者: duanyongxing
  113. * 時間 : 2011-12-04
  114. *********************************************************************/
  115. int main(int argc, char *argv[])
  116. {
  117. int iRet = SOC_ERR;
  118. Write_Log(LOG_TYPE_INFO, "func main entering...");
  119. /*打開數據庫*/
  120. iRet = sqlite3_open(g_szdbPath, &g_pdb);
  121. if (SQLITE_OK != iRet)
  122. {
  123. Write_Log(LOG_TYPE_ERROR, "open db return error. iRet = %d, db_path = %s\n", iRet, g_szdbPath);
  124. return SOC_ERR;
  125. }
  126. Write_Log(LOG_TYPE_INFO, "open db succ.");
  127. /*操作數據庫*/
  128. iRet = opeate_tbl_product(g_pdb);
  129. if (SOC_OK != iRet)
  130. {
  131. Write_Log(LOG_TYPE_ERROR, "call opeate_tbl_product return error.", iRet);
  132. return SOC_ERR;
  133. }
  134. /*關閉數據庫*/
  135. iRet = sqlite3_close(g_pdb);
  136. if (SQLITE_OK != iRet)
  137. {
  138. Write_Log(LOG_TYPE_ERROR, "close db return error. iRet = %d, db_path = %s", iRet, g_szdbPath);
  139. return SOC_ERR;
  140. }
  141. Write_Log(LOG_TYPE_INFO, "func main leaing...");
  142. return SOC_OK;
  143. }

sqlite_golbal.c

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

執行結果:

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...

其他更多操作見。http://www.linuxidc.com/Linux/2012-02/54212.htm 與http://www.linuxidc.com/Linux/2012-02/54213.htm

Copyright © Linux教程網 All Rights Reserved