在android 系统中,Sqlite数据库可以用来存储数据,在c层和android层都分别有接口来对数据库文件进行增删改查的操作,如果灵活运用会解决一些问题。
例如 可在android层利用contentprovider对数据库进行增删改查,在c层也对此数据库进行增删改查,通常c层操作在contentprovider未运行起来之前操作,数据库文件保存在单独的一个flash分区内,系统recovery时不擦除此分区,数据库文件就可以保存下来,如此,可以提高用户的体验效果。
本文主要记录这一思想,并且附上c操作的简单代码例子。
- #include <stdio.h>
- #include <string.h>
- #include "sqlite3.h"
- #include <stdlib.h>
- #define CHARLTH 128
- #define SQLTH 256
- #define DB_PATH "/data/Mydb/"
- #define DB_NAME "My_db"
- #define ESC 27
- #define DBCREATE "create table My(id integer primary key autoincrement,MyName text,MyValues text,DefaultValues text ,BeDelete int,BeDefault int);"
- int main(){
- char ch;
- void toast();
- int query( const char *key,char *val);
- int update( const char *name,char *value);
- int add(const char *MyName,const char *val,const int flgdlte,const int flagrevert);
- int delete( const char *key);
- toast();
- while((ch=getchar()) != ESC){
- int i=-2;
- char MyName[CHARLTH];
- memset(MyName,0,sizeof(MyName));
- char val[CHARLTH];
- memset(val,0,sizeof(val));
- int flgdle;
- int flagrevert;
- switch(ch){
- case 'a':
- printf("input a to addvalue ,scanf MyName! \n");
- scanf("%s",MyName);
- printf("input MyName:%s,then scanf MyValue!\n",MyName);
- scanf("%s",val);
- printf("input MyValue:%s,then scanf deleteflag 1 can delete,0 can't delete.\n",val);
- scanf("%d",&flgdle);
- printf("input DeleteFlag is: %d,scanf revertOnboot 1 can revert, 0 not revert. \n",flgdle);
- if( (1 !=flgdle) &&(0 !=flgdle)){
- printf("No valid input , Exit!\n");
- exit(0);
- }
- scanf("%d",&flagrevert);
- printf("input revertOnbootFlag:%d\n",flagrevert);
- if((1 !=flagrevert) && (0 !=flagrevert)){
- printf("No valid input , Exit!\n");
- exit(0);
- }
- i = add((char*)MyName,(char*)val,flgdle,flagrevert);
- if(0 == i){
- printf("add success!\n");
- }else {
- printf("add error\n");
- }
- break;
- case 'd':
- printf("input d to delete ,scanf MyName. \n");
- scanf("%s",MyName);
- printf("the input MyName:%s\n",MyName);
- i = delete((char*)MyName);
- if(0 == i){
- printf("delete success!\n");
- }else{
- printf("delete error!\n");
- }
- break;
- case 'm':
- printf("input m to modify MyValue ,scanf MyName.\n");
- scanf("%s",MyName);
- printf("the input MyName:%s scanf New value\n",MyName);
- scanf("%s",val);
- printf("the input MyValue:%s\n",val);
- i = update((char*)MyName,(char*)val);
- if(0 == i){
- printf("modify success!\n");
- }else {
- printf("modify error\n");
- }
- break;
- case 'q':
- printf("input q to querry MyValue,scanf MyName.\n");
- scanf("%s",MyName);
- // printf("main::: scanf::: %s\n",MyName); // test scanf right
- i = query((char*)MyName,val);
- if(0 == i){
- printf("%s:::%s\n",MyName,val);
- }else {
- printf("error\n");
- }
- break;
- case 'h':
- printf("input h to get help\n");
- toast();
- break;
- default:
- //printf("No valid input!\n");
- break;
- }
- }
- return 0;
- }
- void toast(){
- printf("****************************************************\n\n");
- printf("Use MyClient to add,delete,modify,query value!\n\n");
- printf("****************************************************\n\n");
- 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");
- printf("****************************************************\n\n");
- }
- int add(const char *MyName,const char *val,const int flgdlte,const int flagrevert){
- int rt = 0;
- sqlite3 *db = NULL ;
- char *zErrMsg = 0;
- char *values = NULL;
- char *db_begin= "BEGIN TRANSACTION;";
- char *db_end = "COMMIT TRANSACTION;";
- int i = 0;
- int nRow = 0;
- int nColumn = 0;
- char** dbResult = NULL;
- int rc = 0;
- char sql[SQLTH];
- char My_name[CHARLTH];
- char My_values[CHARLTH];
- int flag_dlte;
- int flag_revert;
- char db_path[SQLTH];
- memset(My_name,0,sizeof(My_name));
- memset(My_values,0,sizeof(My_values));
- strcpy(My_name,MyName);
- strcpy(My_values,val);
- flag_dlte = flgdlte;
- flag_revert = flagrevert;
- memset(sql,0,sizeof(sql));
- sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
- //rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READWRITE,NULL);
- rc = sqlite3_open(db_path,&db);
- if( rc ) {
- printf("open error ,can't open database\n");
- fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
- rt = -1;
- sqlite3_close(db);
- return rt;
- }
- rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);
- sprintf(sql,"select *from My where MyName = '%s';",My_name);
- rc = sqlite3_exec(db,DBCREATE,NULL,NULL,NULL);
- if(SQLITE_OK != rc){
- // sqlite3_close(db);
- // sqlite3_free(errmsg);
- printf("create db table error \n");
- rt = -1;
- }
- rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);
- if (0 == nRow){
- rt = -1;
- //sqlite3_free_table(dbResult);
- //sqlite3_close(db);
- //return rt;
- memset(sql,0,sizeof(sql));
- sprintf(sql,"insert into My(MyName,MyValues,DefaultValues,BeDelete,BeDefault)values \
- ('%s','%s','%s','%d','%d');",My_name,My_values,My_values,flag_dlte,flag_revert);
- rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
- if(0 != rc){
- printf("My add value Error!\n");
- rt = -1;
- }else{
- rt = 0;
- }
- }else{
- // not 0 == nRow the Myname has been stored in My.
- }
- sqlite3_free_table(dbResult);
- rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
- sqlite3_close(db);
- return rt;
- }
- int update( const char *key,char *val){
- int rt = 0;
- sqlite3 *db = NULL ;
- char *zErrMsg = 0;
- char *values = NULL;
- char *db_begin= "BEGIN TRANSACTION;";
- char *db_end = "COMMIT TRANSACTION;";
- int i = 0;
- int nRow = 0;
- int nColumn = 0;
- char** dbResult = NULL;
- int rc = 0;
- char sql[SQLTH];
- char My_values[CHARLTH];
- char db_path[SQLTH];
- char MyName[CHARLTH];
- memset(sql,0,sizeof(sql));
- memset(MyName,0,sizeof(MyName));
- memset(My_values,0,sizeof(My_values));
- memset(db_path,0,sizeof(db_path));
- if(NULL==key || NULL==val){
- rt = -1;
- return rt;
- }
- sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
- // printf("My db_path::::::::%s\n",db_path);
- rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READWRITE,NULL);
- if( rc ) {
- fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
- rt = -1;
- sqlite3_close(db);
- return rt;
- }
- rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);
- strcpy(MyName,key);
- strcpy(My_values,val);
- // printf("query::::MyName:::%s\n",MyName); // test go to query is right or not
- sprintf(sql,"select *from My where MyName = '%s';",MyName);
- rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);
- if (0 == nRow){
- rt = -1;
- sqlite3_free_table(dbResult);
- sqlite3_close(db);
- return rt;
- }
- sprintf(sql,"update My set MyValues= '%s' where MyName = '%s';",My_values,MyName);
- //printf("update sql:%s\n",sql);
- rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
- //printf("update rc sql %d\n",rc);
- if(0 != rc){
- rt = -1;
- sqlite3_close(db);
- return rt;
- }
- rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
- sqlite3_close(db);
- if(0 != rc){
- rt = -1;
- }
- return rt;
- }
- int delete( const char *key){
- int rt = 0;
- sqlite3 *db = NULL ;
- char *zErrMsg = 0;
- // char *values = NULL;
- char *db_begin= "BEGIN TRANSACTION;";
- char *db_end = "COMMIT TRANSACTION;";
- int nRow = 0;
- int nColumn = 0;
- int i = 0;
- char** dbResult = NULL;
- int rc = 0;
- char sql[SQLTH];
- char db_path[SQLTH];
- char MyName[CHARLTH];
- memset(sql,0,sizeof(sql));
- memset(MyName,0,sizeof(MyName));
- memset(db_path,0,sizeof(db_path));
- if(NULL==key ){
- rt = -1;
- return rt;
- }
- sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
- // printf("My db_path::::::::%s\n",db_path);
- rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READWRITE,NULL);
- if( rc ) {
- fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
- rt = -1;
- sqlite3_close(db);
- return rt;
- }
- rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);
- strcpy(MyName,key);
- //printf("query::::MyName:::%s\n",MyName); // test go to query is right or not
- sprintf(sql,"select *from My where MyName = '%s';",MyName);
- rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);
- if (0 == nRow){
- rt = -1;
- sqlite3_free_table(dbResult);
- sqlite3_close(db);
- return rt;
- }
- char *values = *(&dbResult[nColumn]+4);
- printf(" values delete flag = %s\n",values);
- //printf(" *values delete flag =%c\n",*values);
- int deleteflag = atoi(values);
- //printf(" delete flag values atoi %d\n",deleteflag);
- // deleteflag = atoi((const char *)(*values));
- // printf("delete flag *values atoi %d\n",deleteflag);
- if(1 == deleteflag){
- //printf("come to delete \n.");
- memset(sql,0,sizeof(sql));
- sprintf(sql,"delete from My where MyName = '%s';",MyName);
- // printf(" sql =%s\n",sql);
- rc = sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
- if (0 != rc){
- rt = -1;
- }else{
- rt = 0;
- }
- }else{
- rt = -1;
- }
- sqlite3_free_table(dbResult);
- rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
- sqlite3_close(db);
- return rt;
- }
- int query( const char *key,char *val){
- int rt = 0;
- sqlite3 *db = NULL ;
- char *zErrMsg = 0;
- char *values = NULL;
- char *db_begin= "BEGIN TRANSACTION;";
- char *db_end = "COMMIT TRANSACTION;";
- int i = 0;
- int nRow = 0;
- int nColumn = 0;
- char** dbResult = NULL;
- int rc = 0;
- char sql[SQLTH];
- char My_values[CHARLTH];
- char db_path[SQLTH];
- char MyName[CHARLTH];
- memset(sql,0,sizeof(sql));
- memset(MyName,0,sizeof(MyName));
- memset(My_values,0,sizeof(My_values));
- memset(db_path,0,sizeof(db_path));
- if(NULL==key || NULL==val){
- rt = -1;
- return rt;
- }
- sprintf(db_path,"%s%s",DB_PATH,DB_NAME);
- //printf("My db_path::::::::%s\n",db_path);
- rc = sqlite3_open_v2(db_path,&db,SQLITE_OPEN_READONLY,NULL);
- if( rc ) {
- fprintf(stderr,"Can't open databases : %s\n",sqlite3_errmsg(db));
- rt = -1;
- sqlite3_close(db);
- return rt;
- }
- rc = sqlite3_exec(db,db_begin,NULL,NULL,NULL);
- strcpy(MyName,key);
- // printf("query::::MyName:::%s\n",MyName); // test go to query is right or not
- sprintf(sql,"select *from My where MyName = '%s';",MyName);
- rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nColumn,&zErrMsg);
- if (0 == nRow){
- rt = -1;
- sqlite3_free_table(dbResult);
- sqlite3_close(db);
- return rt;
- }
- values = *(&dbResult[nColumn]+2);
- while( *values){
- My_values[i] = *values;
- i++;
- values++;
- }
- My_values[i] = '\0';
- // printf("dbquery::My_values :%s\n",My_values); //test
- memset(val,0,sizeof(val));
- strcpy(val,My_values);
- // printf("db_query::val::%s\n",val);
- sqlite3_free_table(dbResult);
- rc = sqlite3_exec(db,db_end,NULL,NULL,NULL);
- sqlite3_close(db);
- rt = 0;
- return rt;
- }
阅读(6517) | 评论(0) | 转发(0) |