找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3398|回复: 0

[译]C/C++用使用SQLITE3及其API接口的基本过程

[复制链接]
发表于 2012-2-29 15:14:52 | 显示全部楼层 |阅读模式
Sqlite是一个面向嵌入式系统的数据库,编译完成只有200K,同时支持2T的数据记录。对于嵌入式设备是一个很好的数据库引擎。本文通过一个小例子说明如何在C与C++调用Sqlite API完成数据库的创建、插入数据与查询数据。本文的开发环境为(Redhat9.0 + Qtopia2.1.2 + Sqlite3) 安装Sqlite3:
www.sqlite.org上下载Sqlite3.2.2运源代码,依照Readme中的步骤:
tar xzf sqlite3.2.2.tar.gz
mkdir bld
cd bld
../sqlite3.2.2/configure
make
make install
然后在shell下运行 sqlite3 test.db命令可以检验是否已经安装成功。
创建数据库:
  1. sqlite3 *pDB = NULL;
  2. char *
  3. errMsg =
  4. NULL;
复制代码

//打开一个数据库,如果改数据库不存在,则创建一个名字为databaseName的数据库文件
  1. int rc = sqlite3_open(databaseName, &pDB);
  2.   
  3. if(rc)
  4.   {
  5.     cout << " Open the
  6. database " << databaseName << " failed" << endl;
  7.   
  8. }
  9.   //如果创建成功,添加表
  10.   else
  11.   {
  12.    
  13. cout << "create the database successful!" <<
  14. endl;
  15.     //creat the table
  16.     int
  17. i;
  18.     for(i=1; i<nTableNum; i++)
  19.    
  20. {
  21.       
  22.     }
复制代码
    //插入一个表,返回值为SQLITE_OK为成功,否则输出出错信息
    //函数参数:第一个为操作数据库的指针,第二句为SQL命令字符串
    //第三个参数为callback函数,这里没有用,第四个参数为callback函数
    //中的第一个参数,第五个为出错信息
  1. rc = sqlite3_exec(pDB, "CREATE TABLE
  2. chn_to_eng(chinese QString, english QString)", 0, 0,
  3. &errMsg);
  4.     if(rc == SQLITE_OK)
  5.       
  6. cout << "create the chn_to_eng table successful!" <<
  7. endl;
  8.     else
  9.        cout << errMsg
  10. << endl;
  11.     //同上,插入另一个表
  12.     rc = sqlite3_exec(pDB, "CREATE TABLE
  13. eng_to_chn(english QString, chinese QString)", 0, 0,
  14. &errMsg);
  15.     if(rc == SQLITE_OK)
  16.         
  17. cout << "create the eng_to_chn table successful!" <<
  18. endl;
  19.     else
  20.        cout << errMsg
  21. << endl;
  22.    
  23.   }
  24.   //往表中添加数据
  25.   char chn[]="...";
  26.   char eng[]="...";
  27.   char
  28. value[500];
  29.   //定义一条参数SQL命令,其中chn,eng为需要插入的数据   
  30.   sprintf(value,
  31. "INSERT INTO chn_to_eng(chinese, english) VALUES('%s', '%s')", chn,
  32. eng);
  33.   
  34.   //use the SQLITE C/C++ API to create
  35. and adjust a database.
  36.   rc =
  37. sqlite3_exec(pDB,
  38.                        
  39. value,
  40.                       0, 0,
  41. &errMsg);
  42. //查询一条记录
  43. char
  44. value[500];
  45. //定义一条查询语句,其中条件为当english为target时的中文记录
  46. //print_result_cb为callback函数,在其中可以得到查询的结果,具体见下文
  47. sprintf(value,
  48. "SELECT chinese FROM eng_to_chn where english='%s' ",
  49. target);
  50.   
  51. rc = sqlite3_exec(pDB,
  52.                      
  53. value,
  54.                       print_result_cb, 0,
  55. &errMsg);
  56.   if(rc == SQLITE_OK)
  57.       
  58. {
  59. //        #ifdef_debug
  60.            cout
  61. << "select the record successful!" << endl;
  62. //        
  63. #endif
  64.       }
  65.       else
  66.       
  67. {
  68. //        #ifdef_debug
  69.           cout
  70. << errMsg << endl;  
  71. //        
  72. #endif                           
  73.          
  74. return false;
  75.       }
  76. .......
  77. }
  78. //callback回调函数print_result_cb的编写。其中第一个参数data为sqlite3_exec中的第四个参数,第二个参数是列的数目,第三个是查询得到的列值,第四个为列的名字。回调函数在每得到一行结果的时候就会执行一次,因为一次性返回全部结果会很占用较多的内存。下面这两个函数输出所有查询到的结果
  79. int print_result_cb(void* data, int n_columns, char**
  80. column_values,
  81.                     char**
  82. column_names)
  83. {
  84.     static int column_names_printed =
  85. 0;
  86.     int i;
  87.     if (!column_names_printed)
  88. {
  89.         print_row(n_columns, column_names);
  90.         
  91. column_names_printed = 1;
  92.     }
  93.    
  94. print_row(n_columns, column_values);
  95.     return
  96. 0;
  97. }
  98. void print_row(int
  99. n_values, char** values)
  100. {
  101.     int i;
  102.     for
  103. (i = 0; i < n_values; ++i) {
  104.         if (i > 0)
  105. {
  106.             printf("\t");
  107.         
  108. }
  109.         printf("%s", values[i]);
  110.         
  111.     }
  112.    
  113. printf("\n");
  114. }
复制代码
大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-16 23:11 , Processed in 0.014398 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表