Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562281
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: C/C++

2012-04-10 21:33:59


点击(此处)折叠或打开

  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,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,sql,0,0,&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,0,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,sql,&sqlite_callback,0,&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",table,id);
  70.     sqlite3_exec(db,sql,&sqlite_callback,0,&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",table,id);
  79.     rc = sqlite3_exec(db,sql,0,0,&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",table);
  87.     sqlite3_exec(db,sql,0,0,&zErrMsg);
  88.     sqlite3_free(sql);
  89. }

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

  94.     create_table(filename);

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

  106.     search_all("save_data");

  107.     close_table();

  108.     return 0;
  109. }


阅读(2032) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~