Chinaunix首页 | 论坛 | 博客
  • 博客访问: 283530
  • 博文数量: 59
  • 博客积分: 235
  • 博客等级: 二等列兵
  • 技术积分: 409
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-12 15:11
文章分类
文章存档

2013年(4)

2012年(55)

分类: SQLite/嵌入式数据库

2013-01-09 21:02:55

 
 
在android 系统中,Sqlite数据库可以用来存储数据,在c层和android层都分别有接口来对数据库文件进行增删改查的操作,如果灵活运用会解决一些问题。
    例如 可在android层利用contentprovider对数据库进行增删改查,在c层也对此数据库进行增删改查,通常c层操作在contentprovider未运行起来之前操作,数据库文件保存在单独的一个flash分区内,系统recovery时不擦除此分区,数据库文件就可以保存下来,如此,可以提高用户的体验效果。
本文主要记录这一思想,并且附上c操作的简单代码例子。

点击(此处)折叠或打开

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

  5. #define CHARLTH 128
  6. #define SQLTH 256
  7. #define DB_PATH "/data/Mydb/"
  8. #define DB_NAME "My_db"
  9. #define ESC 27
  10. #define DBCREATE "create table My(id integer primary key autoincrement,MyName text,MyValues text,DefaultValues text ,BeDelete int,BeDefault int);"

  11. int main(){

  12.     char ch;
  13.     void toast();
  14.     int query( const char *key,char *val);
  15.     int update( const char *name,char *value);
  16.     int add(const char *MyName,const char *val,const int flgdlte,const int flagrevert);
  17.     int delete( const char *key);
  18.     toast();


  19.     while((ch=getchar()) != ESC){

  20.         int i=-2;
  21.         char MyName[CHARLTH];
  22.         memset(MyName,0,sizeof(MyName));
  23.         char val[CHARLTH];
  24.         memset(val,0,sizeof(val));
  25.         int flgdle;
  26.         int flagrevert;

  27.         switch(ch){
  28.             case 'a':
  29.                 printf("input a to addvalue ,scanf MyName! \n");
  30.                 scanf("%s",MyName);
  31.                 printf("input MyName:%s,then scanf MyValue!\n",MyName);
  32.                 scanf("%s",val);
  33.                 printf("input MyValue:%s,then scanf deleteflag 1 can delete,0 can't delete.\n",val);
  34.                 scanf("%d",&flgdle);
  35.                 printf("input DeleteFlag is: %d,scanf revertOnboot 1 can revert, 0 not revert. \n",flgdle);
  36.                 if( (1 !=flgdle) &&(0 !=flgdle)){
  37.                     printf("No valid input , Exit!\n");
  38.                     exit(0);
  39.                 }
  40.                 scanf("%d",&flagrevert);
  41.                 printf("input revertOnbootFlag:%d\n",flagrevert);
  42.                 if((1 !=flagrevert) && (0 !=flagrevert)){
  43.                     printf("No valid input , Exit!\n");
  44.                     exit(0);
  45.                 }
  46.                 i = add((char*)MyName,(char*)val,flgdle,flagrevert);
  47.                 if(0 == i){
  48.                     printf("add success!\n");
  49.                 }else {
  50.                     printf("add error\n");
  51.                 }

  52.                 break;

  53.             case 'd':
  54.                 printf("input d to delete ,scanf MyName. \n");
  55.                 scanf("%s",MyName);
  56.                 printf("the input MyName:%s\n",MyName);
  57.                 i = delete((char*)MyName);
  58.                 if(0 == i){
  59.                     printf("delete success!\n");
  60.                 }else{
  61.                     printf("delete error!\n");
  62.                 }
  63.                 break;

  64.             case 'm':
  65.                 printf("input m to modify MyValue ,scanf MyName.\n");
  66.                 scanf("%s",MyName);
  67.                 printf("the input MyName:%s scanf New value\n",MyName);
  68.                 scanf("%s",val);
  69.                 printf("the input MyValue:%s\n",val);
  70.                 i = update((char*)MyName,(char*)val);
  71.                 if(0 == i){
  72.                     printf("modify success!\n");
  73.                 }else {
  74.                     printf("modify error\n");
  75.                 }

  76.                 break;

  77.             case 'q':
  78.                 printf("input q to querry MyValue,scanf MyName.\n");
  79.                 scanf("%s",MyName);

  80.                 //            printf("main::: scanf::: %s\n",MyName); // test scanf right

  81.                 i = query((char*)MyName,val);
  82.                 if(0 == i){
  83.                     printf("%s:::%s\n",MyName,val);
  84.                 }else {
  85.                     printf("error\n");
  86.                 }
  87.                 break;

  88.             case 'h':
  89.                 printf("input h to get help\n");
  90.                 toast();
  91.                 break;

  92.             default:
  93.                 //printf("No valid input!\n");
  94.                 break;
  95.         }


  96.     }
  97.            return 0;
  98. }


  99. void toast(){
  100.     printf("****************************************************\n\n");
  101.     printf("Use MyClient to add,delete,modify,query value!\n\n");
  102.     printf("****************************************************\n\n");
  103.     printf("input \n a to add value \n d to delete value \n m to modify value \n q to query value \n ESC to queit \n h to get help\n");
  104.     printf("****************************************************\n\n");

  105. }



  106. int add(const char *MyName,const char *val,const int flgdlte,const int flagrevert){

  107.     int rt = 0;
  108.     sqlite3 *db = NULL ;
  109.     char *zErrMsg = 0;
  110.     char *values = NULL;
  111.     char *db_begin= "BEGIN TRANSACTION;";
  112.     char *db_end = "COMMIT TRANSACTION;";

  113.     int i = 0;
  114.     int nRow = 0;
  115.     int nColumn = 0;
  116.     char** dbResult = NULL;
  117.     int rc = 0;
  118.     char sql[SQLTH];
  119.     char My_name[CHARLTH];
  120.     char My_values[CHARLTH];
  121.     int flag_dlte;
  122.     int flag_revert;
  123.     char db_path[SQLTH];


  124.     memset(My_name,0,sizeof(My_name));
  125.     memset(My_values,0,sizeof(My_values));
  126.     strcpy(My_name,MyName);
  127.     strcpy(My_values,val);

  128.     flag_dlte = flgdlte;
  129.     flag_revert = flagrevert;



  130.     memset(sql,0,sizeof(sql));
  131.     sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
  132.     //rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READWRITE,NULL);
  133.     rc = sqlite3_open(db_path,&db);
  134.     if( rc ) {
  135.         printf("open error ,can't open database\n");
  136.         fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
  137.         rt = -1;
  138.         sqlite3_close(db);
  139.         return rt;
  140.     }

  141.     rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);
  142.     sprintf(sql,"select *from My where MyName = '%s';",My_name);
  143.     rc = sqlite3_exec(db,DBCREATE,NULL,NULL,NULL);
  144.         if(SQLITE_OK != rc){
  145.            // sqlite3_close(db);
  146.            // sqlite3_free(errmsg);
  147.            printf("create db table error \n");
  148.             rt = -1;
  149.     }
  150.     rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);
  151.     if (0 == nRow){
  152.         rt = -1;
  153.         //sqlite3_free_table(dbResult);
  154.         //sqlite3_close(db);
  155.         //return rt;
  156.         memset(sql,0,sizeof(sql));
  157.         sprintf(sql,"insert into My(MyName,MyValues,DefaultValues,BeDelete,BeDefault)values \
  158.                 ('%s','%s','%s','%d','%d');",My_name,My_values,My_values,flag_dlte,flag_revert);
  159.         rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
  160.         if(0 != rc){
  161.             printf("My add value Error!\n");
  162.             rt = -1;
  163.         }else{
  164.             rt = 0;
  165.         }
  166.     }else{
  167.         // not 0 == nRow the Myname has been stored in My.

  168.     }
  169.     sqlite3_free_table(dbResult);
  170.     rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
  171.     sqlite3_close(db);

  172.     return rt;

  173. }


  174. int update( const char *key,char *val){
  175.     int rt = 0;
  176.     sqlite3 *db = NULL ;
  177.     char *zErrMsg = 0;
  178.     char *values = NULL;
  179.     char *db_begin= "BEGIN TRANSACTION;";
  180.     char *db_end = "COMMIT TRANSACTION;";

  181.     int i = 0;
  182.     int nRow = 0;
  183.     int nColumn = 0;
  184.     char** dbResult = NULL;
  185.     int rc = 0;
  186.     char sql[SQLTH];
  187.     char My_values[CHARLTH];
  188.     char db_path[SQLTH];
  189.     char MyName[CHARLTH];

  190.     memset(sql,0,sizeof(sql));
  191.     memset(MyName,0,sizeof(MyName));
  192.     memset(My_values,0,sizeof(My_values));
  193.     memset(db_path,0,sizeof(db_path));

  194.     if(NULL==key || NULL==val){
  195.         rt = -1;
  196.         return rt;
  197.     }

  198.     sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
  199.     //    printf("My db_path::::::::%s\n",db_path);

  200.     rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READWRITE,NULL);

  201.     if( rc ) {
  202.         fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
  203.         rt = -1;
  204.         sqlite3_close(db);
  205.         return rt;
  206.     }

  207.     rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);

  208.     strcpy(MyName,key);
  209.     strcpy(My_values,val);
  210.     //    printf("query::::MyName:::%s\n",MyName); // test go to query is right or not

  211.     sprintf(sql,"select *from My where MyName = '%s';",MyName);
  212.     rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);
  213.     if (0 == nRow){
  214.         rt = -1;
  215.         sqlite3_free_table(dbResult);
  216.         sqlite3_close(db);
  217.         return rt;
  218.     }

  219.     sprintf(sql,"update My set MyValues= '%s' where MyName = '%s';",My_values,MyName);

  220.     //printf("update sql:%s\n",sql);
  221.     rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
  222.        //printf("update rc sql %d\n",rc);
  223.     if(0 != rc){
  224.         rt = -1;
  225.         sqlite3_close(db);
  226.         return rt;

  227.     }

  228.     rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
  229.     sqlite3_close(db);
  230.     if(0 != rc){
  231.         rt = -1;
  232.     }

  233.     return rt;


  234. }



  235. int delete( const char *key){
  236.     int rt = 0;
  237.     sqlite3 *db = NULL ;
  238.     char *zErrMsg = 0;
  239.     //    char *values = NULL;
  240.     char *db_begin= "BEGIN TRANSACTION;";
  241.     char *db_end = "COMMIT TRANSACTION;";


  242.     int nRow = 0;
  243.     int nColumn = 0;

  244.     int i = 0;
  245.     char** dbResult = NULL;
  246.     int rc = 0;
  247.     char sql[SQLTH];
  248.     char db_path[SQLTH];
  249.     char MyName[CHARLTH];

  250.     memset(sql,0,sizeof(sql));
  251.     memset(MyName,0,sizeof(MyName));
  252.     memset(db_path,0,sizeof(db_path));

  253.     if(NULL==key ){
  254.         rt = -1;
  255.         return rt;
  256.     }


  257.     sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
  258.     //    printf("My db_path::::::::%s\n",db_path);

  259.     rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READWRITE,NULL);

  260.     if( rc ) {
  261.         fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
  262.         rt = -1;
  263.         sqlite3_close(db);
  264.         return rt;
  265.     }

  266.     rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);

  267.     strcpy(MyName,key);

  268.     //printf("query::::MyName:::%s\n",MyName); // test go to query is right or not

  269.     sprintf(sql,"select *from My where MyName = '%s';",MyName);
  270.     rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);

  271.     if (0 == nRow){
  272.         rt = -1;
  273.         sqlite3_free_table(dbResult);
  274.         sqlite3_close(db);
  275.         return rt;
  276.     }

  277.     char    *values = *(&dbResult[nColumn]+4);

  278.     printf(" values delete flag = %s\n",values);
  279.     //printf(" *values delete flag =%c\n",*values);

  280.     int deleteflag = atoi(values);
  281.     //printf(" delete flag values atoi %d\n",deleteflag);
  282.     // deleteflag = atoi((const char *)(*values));
  283.     // printf("delete flag *values atoi %d\n",deleteflag);

  284.     if(1 == deleteflag){
  285.          //printf("come to delete \n.");
  286.         memset(sql,0,sizeof(sql));
  287.         sprintf(sql,"delete from My where MyName = '%s';",MyName);
  288.         //    printf(" sql =%s\n",sql);
  289.         rc = sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);

  290.         if (0 != rc){
  291.             rt = -1;
  292.         }else{
  293.             rt = 0;
  294.         }

  295.     }else{
  296.         rt = -1;
  297.     }
  298.     sqlite3_free_table(dbResult);
  299.     rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
  300.     sqlite3_close(db);

  301.     return rt;

  302. }


  303. int query( const char *key,char *val){
  304.     int rt = 0;
  305.     sqlite3 *db = NULL ;
  306.     char *zErrMsg = 0;
  307.     char *values = NULL;
  308.     char *db_begin= "BEGIN TRANSACTION;";
  309.     char *db_end = "COMMIT TRANSACTION;";

  310.     int i = 0;
  311.     int nRow = 0;
  312.     int nColumn = 0;
  313.     char** dbResult = NULL;
  314.     int rc = 0;
  315.     char sql[SQLTH];
  316.     char My_values[CHARLTH];
  317.     char db_path[SQLTH];
  318.     char MyName[CHARLTH];

  319.     memset(sql,0,sizeof(sql));
  320.     memset(MyName,0,sizeof(MyName));
  321.     memset(My_values,0,sizeof(My_values));
  322.     memset(db_path,0,sizeof(db_path));

  323.     if(NULL==key || NULL==val){
  324.         rt = -1;
  325.         return rt;
  326.     }

  327.     sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
  328.     //printf("My db_path::::::::%s\n",db_path);

  329.     rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READONLY,NULL);

  330.     if( rc ) {
  331.         fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
  332.         rt = -1;
  333.         sqlite3_close(db);
  334.         return rt;
  335.     }

  336.     rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);

  337.     strcpy(MyName,key);

  338.     //    printf("query::::MyName:::%s\n",MyName); // test go to query is right or not

  339.     sprintf(sql,"select *from My where MyName = '%s';",MyName);
  340.     rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);

  341.     if (0 == nRow){
  342.         rt = -1;
  343.         sqlite3_free_table(dbResult);
  344.         sqlite3_close(db);
  345.         return rt;
  346.     }

  347.     values = *(&dbResult[nColumn]+2);

  348.     while( *values){
  349.         My_values[i] = *values;
  350.         i++;
  351.         values++;
  352.     }
  353.     My_values[i] = '\0';

  354.     //    printf("dbquery::My_values :%s\n",My_values); //test

  355.     memset(val,0,sizeof(val));
  356.     strcpy(val,My_values);

  357.     //    printf("db_query::val::%s\n",val);

  358.     sqlite3_free_table(dbResult);
  359.     rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
  360.     sqlite3_close(db);
  361.     rt = 0;

  362.     return rt;


  363. }

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