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

前端之家收集整理的这篇文章主要介绍了sqlite3的基本操作(插入,删除,更新,查询)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


  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%s\n",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 %d\n",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 %d\n",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. }
原文链接:https://www.f2er.com/sqlite/200814.html

猜你在找的Sqlite相关文章