分享

<2>sqlite3数据库c语言sqlite3_prepare_v2插入数据

 XeonGate 2019-11-07
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sqlite3.h>
  4. /***************************
  5. typedef int (*sqlite3_callback)(
  6. void*, // Data provided in the 4th argument of sqlite3_exec()
  7. int, // The number of columns in row
  8. char**, // An array of strings representing fields in the row
  9. char** // An array of strings representing column names
  10. );
  11. ***************************/
  12. /* callback函数只有在对数据库进行select, 操作时才会调用 */
  13. static int select_callback(void *data, int argc, char **argv, char **azColName){
  14. int i;
  15. printf("%s", (char*)data);
  16. for(i=0; i < argc; i++){
  17. printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  18. }
  19. printf("\n");
  20. return 0;
  21. }
  22. int main(int argc, char* argv[])
  23. {
  24. sqlite3 *db;
  25. char *zErrMsg = 0;
  26. int rc;
  27. /* 数据库创建或打开 */
  28. rc = sqlite3_open("test.db", &db);
  29. if( rc ){
  30. fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  31. exit(0);
  32. }else{
  33. fprintf(stderr, "Opened database successfully\n");
  34. }
  35. char* sql;
  36. sql = "create table healthinfo (" \
  37. "sid int primary key not null," \
  38. "name text not null," \
  39. "ishealth char(4) not null);";
  40. /* 创建表 */
  41. rc = sqlite3_exec(db, sql, NULL, NULL, &zErrMsg);
  42. if( rc != SQLITE_OK ){
  43. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  44. sqlite3_free(zErrMsg);
  45. }else{
  46. fprintf(stdout, "Table created successfully\n");
  47. }
  48. sql = "insert into healthinfo (sid, name, ishealth)" \
  49. "values (201601001, 'xiaowang', 'yes');" \
  50. "insert into healthinfo (sid, name, ishealth)" \
  51. "values (201601002, 'xiaoli', 'yes');" \
  52. "insert into healthinfo (sid, name, ishealth)" \
  53. "values (201601003, 'xiaozhang', 'no');" \
  54. "insert into healthinfo (sid, name, ishealth)" \
  55. "values (201601004, 'xiaozhou', 'yes');" \
  56. "insert into healthinfo (sid, name, ishealth)" \
  57. "values (201601005, 'xiaosun', 'yes');";
  58. /* 插入数据 */
  59. rc = sqlite3_exec(db, sql, NULL, NULL, &zErrMsg);
  60. if( rc != SQLITE_OK ){
  61. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  62. sqlite3_free(zErrMsg);
  63. }else{
  64. fprintf(stdout, "Table insert data successfully\n");
  65. }
  66. char* strname = "xiaoyang";
  67. //char strname[256] = {'x','i','a','o','y','a','n','g'};
  68. char sql2[256] = {'0'};
  69. /* 不推荐使用这种方式 */
  70. sprintf(sql2, "insert into healthinfo (sid, name, ishealth) values (201601006, '%s', 'yes');", strname);
  71. /* 插入数据 */
  72. rc = sqlite3_exec(db, sql2, NULL, NULL, &zErrMsg);
  73. if( rc != SQLITE_OK ){
  74. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  75. sqlite3_free(zErrMsg);
  76. }else{
  77. fprintf(stdout, "Table insert data successfully\n");
  78. }
  79. /*********** 存数据和取数据的第二种方法***********/
  80. sql = "insert into healthinfo (sid, name, ishealth)" \
  81. "values (:sid, :name, :ishealth);"; /* 注: ":sid" 为命名参数 也可以用? 号*/
  82. sqlite3_stmt *stmt;
  83. /* 准备一个语句对象 */
  84. sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
  85. /* 语句对象绑定的参数个数也就是上面sql语句values括号中的参数 */
  86. printf("max_parameter_count = %d\n", sqlite3_bind_parameter_count(stmt));
  87. /* 只有上面指定了:sid这个名字才可以用 */
  88. printf("sid parameter index = %d\n", sqlite3_bind_parameter_index(stmt, ":sid"));
  89. printf("name parameter index = %d\n", sqlite3_bind_parameter_index(stmt, ":name"));
  90. printf("ishealth parameter index = %d\n", sqlite3_bind_parameter_index(stmt, ":ishealth"));
  91. /* 如果是?号命名的则返回的文本为null */
  92. printf("index = 1 's parameter's name = %s\n", sqlite3_bind_parameter_name(stmt, 1));
  93. sqlite3_bind_int(stmt, 1, 201601007);
  94. sqlite3_bind_text(stmt, 2, "xiaoqian", -1, NULL); /* 第四个参数设为负数则自动计算第三个参数的长度 */
  95. sqlite3_bind_text(stmt, 3, "yes", 3, NULL);
  96. //sqlite3_bind_blob(stmt, 1, sectionData, 4096, SQLITE_STATIC); /* 将sectonData 绑定到stmt对象 */
  97. /* 执行sql 语句对象并判断其返回值
  98. 发现如果不是select 这样会产生结果集的操作
  99. 返回值为SQLITE_DONE 或者出错,只有执行sql语句会产生
  100. 结果集执行step函数才返回SQLITE_ROW*/
  101. rc = sqlite3_step(stmt);
  102. printf("step() return %s\n", rc == SQLITE_DONE ? "SQLITE_DONE" \
  103. : rc == SQLITE_ROW ? "SQLITE_ROW" : "SQLITE_ERROR");
  104. sqlite3_reset(stmt); /* 如果要重新绑定其他值要reset一下 */
  105. sqlite3_bind_int(stmt, 1, 201601008);
  106. sqlite3_bind_text(stmt, 2, "xiaowu", -1, NULL); /* 重新绑定值 */
  107. sqlite3_bind_text(stmt, 3, "yes", 3, NULL);
  108. sqlite3_step(stmt); /* 再执行 */
  109. /* 销毁prepare 创建的语句对象 */
  110. sqlite3_finalize(stmt);
  111. /* 取数据 */
  112. //sql = "select * from healthinfo;";
  113. sql = "select * from healthinfo limit 4 offset 2;"; /* 限制返回4行且从第3行开始 */
  114. sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
  115. printf("total_column = %d\n", sqlite3_column_count(stmt));
  116. /* 遍历执行sql语句后的结果集的每一行数据 */
  117. while(sqlite3_step(stmt) == SQLITE_ROW){
  118. /* 获得字节数,第二个参数为select结果集合的列号 */
  119. /* 由于select 的结果集只有section这一列,因此为0 */
  120. int len_sid = sqlite3_column_bytes(stmt, 0);
  121. int len_name = sqlite3_column_bytes(stmt, 1);
  122. int len_ishealth = sqlite3_column_bytes(stmt, 2);
  123. printf("sid = %d, len = %d\n", sqlite3_column_int(stmt, 0), len_sid);
  124. printf("name = %s, len = %d\n", sqlite3_column_text(stmt, 1), len_name);
  125. printf("ishealth = %s, len = %d\n", sqlite3_column_text(stmt, 2), len_ishealth);
  126. //unsigned char* srcdata = sqlite3_column_blob(stmt, 0); /* 取得数据库中的blob数据 */
  127. }
  128. printf("\n");
  129. sqlite3_finalize(stmt);
  130. /******************* end ****************************/
  131. const char* data = "select call back function call!\n";
  132. /* select 使用*/
  133. sql = "select * from healthinfo where ishealth == 'yes';";
  134. rc = sqlite3_exec(db, sql, select_callback, data, &zErrMsg);
  135. if( rc != SQLITE_OK ){
  136. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  137. sqlite3_free(zErrMsg);
  138. }else{
  139. fprintf(stdout, "Table select successfully\n");
  140. }
  141. data = "update call back function call!\n";
  142. /* update 使用*/
  143. sql = "update healthinfo set ishealth = 'no' where name='xiaoli';" \
  144. "select * from healthinfo where ishealth == 'yes';";
  145. rc = sqlite3_exec(db, sql, select_callback, data, &zErrMsg);
  146. if( rc != SQLITE_OK ){
  147. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  148. sqlite3_free(zErrMsg);
  149. }else{
  150. fprintf(stdout, "Table update successfully\n");
  151. }
  152. /* 删除表 */
  153. sql = "drop table healthinfo;";
  154. rc = sqlite3_exec(db, sql, NULL, NULL, &zErrMsg);
  155. if( rc != SQLITE_OK ){
  156. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  157. sqlite3_free(zErrMsg);
  158. }else{
  159. fprintf(stdout, "Table droped successfully\n");
  160. }
  161. char sql5[256];
  162. char* tname = "abc";
  163. sprintf(sql5, "create table if not exists %s ("\
  164. "id int not null," \
  165. "name text not null);", tname);
  166. printf("%s\n", sql5);
  167. /* 创建表 */
  168. rc = sqlite3_exec(db, sql5, NULL, NULL, &zErrMsg);
  169. if( rc != SQLITE_OK ){
  170. fprintf(stderr, "SQL error: %s\n", zErrMsg);
  171. sqlite3_free(zErrMsg);
  172. }else{
  173. fprintf(stdout, "Table created successfully\n");
  174. }
  175. sqlite3_close(db);
  176. }

学了一天的sqlite3编程,学习用例贴出来,基本的函数都用到了,也进行了详细的说明,希望对看的人有帮助。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多