加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite3的基本操作(插入,删除,更新,查询)

发布时间:2020-12-12 20:07:42 所属栏目:百科 来源:网络整理
导读:sqlite3的基本操作(插入,删除,更新,查询) 2013-10-08 22:31:33 #include stdio . h #include stdlib . h #include string . h #include "sqlite3.h" sqlite3 * db = NULL ; static int sn = 0 ; void create_table ( char * filename ) { char * sql ;
sqlite3的基本操作(插入,删除,更新,查询) 2013-10-08 22:31:33

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "sqlite3.h"

  5. sqlite3 * db = NULL;

  6. static int sn = 0;

  7. void create_table(char * filename)
  8. {
  9. char * sql;
  10. char * zErrMsg = 0;
  11. int rc;

  12. rc = sqlite3_open(filename,&db);
  13. //rc = sqlite3_open_v2(filename,&db,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
  14. if (rc)
  15. {
  16. fprintf(stderr,"can't open database%sn",sqlite3_errmsg(db));
  17. sqlite3_close(db);
  18. }
  19. sql = "CREATE TABLE save_data(num integer primary key,id int,data text,time text)";
  20. sqlite3_exec(db,sql,0,&zErrMsg);
  21. }

  22. void close_table(void)
  23. {
  24. sqlite3_close(db);
  25. }


  26. void insert_record(char * table,int id,char * data,char * time)
  27. {
  28. char * sql;
  29. char * zErrMsg = NULL;

  30. sql = sqlite3_mprintf("insert into %s values(null,%d,'%s','%s')",table,id,data,time);
  31. sqlite3_exec(db,&zErrMsg);
  32. sqlite3_free(sql);
  33. }

  34. int sqlite_callback(void * userData,int numCol,char ** colData,char **colName)
  35. {
  36. int i,offset = 0;
  37. char * buf,* tmp;

  38. buf = (char *)malloc(40 * sizeof(char));
  39. tmp = buf;
  40. memset(buf,40);

  41. //printf("%d %dn",sizeof(buf),strlen(buf));
  42. for (i = 1;i < numCol;i++)
  43. {
  44. buf = buf + offset;
  45. sprintf(buf,"%s ",colData[i]);
  46. offset = strlen(colData[i]) + 1; //it's need one place for put a blank so the lenght add 1
  47. //printf("i %d offset %dn",i, offset);
  48. }
  49. printf("%.4d. %s n",++sn,tmp);

  50. free(tmp);
  51. tmp = NULL;
  52. buf = NULL;

  53. return 0;
  54. }


  55. void search_all(char * table)
  56. {
  57. char * sql;
  58. char * zErrMsg = 0;

  59. sn = 0;

  60. sql = sqlite3_mprintf("select * from %s",table);
  61. sqlite3_exec(db,&sqlite_callback,&zErrMsg);
  62. sqlite3_free(sql);

  63. }

  64. void search_by_id(char * table,char * id)
  65. {
  66. char * sql;
  67. char * zErrMsg = 0;

  68. sn = 0;

  69. sql = sqlite3_mprintf("select * from %s where id=%s",id);
  70. sqlite3_exec(db,&zErrMsg);
  71. sqlite3_free(sql);
  72. }

  73. void delete_by_id(char * table,char * id)
  74. {
  75. int rc ;
  76. char * sql;
  77. char * zErrMsg = 0;
  78. sql = sqlite3_mprintf("delete from %s where id=%s",id);
  79. rc = sqlite3_exec(db,&zErrMsg);
  80. sqlite3_free(sql);
  81. }

  82. void delete_all(char * table)
  83. {
  84. char * sql;
  85. char * zErrMsg = 0;

  86. sql = sqlite3_mprintf("delete from %s",&zErrMsg);
  87. sqlite3_free(sql);
  88. }

  89. int main(int agrc,char * argv[])
  90. {
  91. char * filename = "data.db";
  92. int i ;

  93. create_table(filename);

  94. for (i = 0 ;i < 10;i++)
  95. {
  96. insert_record("save_data",2000,"5678","2012-03-12 09:43:56");
  97. insert_record("save_data",2001,2002,2003,2004,2005,2006,2007,"2012-03-12 09:43:56");
  98. }

  99. search_all("save_data");

  100. close_table();

  101. return 0;
  102. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读