Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3550795
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类: C/C++

2013-07-19 00:52:04


点击(此处)折叠或打开

  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. }


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