iOS 数据库持久化

时间:2023-03-09 20:57:45
iOS 数据库持久化

Java代码

  1. -(void) addObserver{
  2. //当程序进入后台时执行操作
  3. UIApplication *app = [UIApplication sharedApplication];
  4. [[ NSNotificationCenter defaultCenter] addObserver: self
  5. selector:@selector(appwillresignActive) name:UIApplicationWillResignActiveNotification object:app];
  6. }
  7. -(void) appwillresignActive{
  8. NSLog(@"监听测试");
  9. }

-(void) addObserver{

//当程序进入后台时执行操作

UIApplication *app = [UIApplication sharedApplication];

[[ NSNotificationCenter defaultCenter] addObserver: self

selector:@selector(appwillresignActive) name:UIApplicationWillResignActiveNotification object:app];

}

-(void) appwillresignActive{

NSLog(@"监听测试");

}

头部引入

Java代码

  1. #import "sqlite3.h"

#import "sqlite3.h"

Java代码

  1. -(void) makeDBinfo{
  2. NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  3. NSString *databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"mydb"];
  4. char *errorMsg;
  5. //打开或创建数据库
  6. sqlite3 *database;
  7. if (sqlite3_open([databaseFilePath UTF8String], &database)!=SQLITE_OK) {
  8. sqlite3_close(database);
  9. }else {
  10. NSLog(@"open sqlite db ok.");
  11. }
  12. //创建数据库表
  13. const char *createSql=" create table if not exists persons (id integer primary key autoincrement,name text)";
  14. if (sqlite3_exec(database, createSql, NULL, NULL, &errorMsg)==SQLITE_OK)
  15. {
  16. NSLog(@"create table ok.");
  17. }else
  18. {
  19. //如果在多个地方使用errorMsg,那么每次使用完毕要清空一下字串,比如这样:
  20. NSLog(@"error: %s",errorMsg);
  21. sqlite3_free(errorMsg);
  22. }
  23. // 向表中插入记录
  24. const char *insertSql="insert into persons (name) values(\"张三\")";
  25. if (sqlite3_exec(database, insertSql, NULL, NULL, &errorMsg)==SQLITE_OK)
  26. {
  27. NSLog(@"insert ok.");
  28. }else
  29. {
  30. //如果在多个地方使用errorMsg,那么每次使用完毕要清空一下字串,比如这样:
  31. NSLog(@"error: %s",errorMsg);
  32. sqlite3_free(errorMsg);
  33. }
  34. //结果集的查询,需要用到statement:
  35. const char *selectSql="select id,name from persons where name = ? ";
  36. sqlite3_stmt *statement;
  37. if (sqlite3_prepare_v2(database, selectSql, -1, &statement, nil)==SQLITE_OK) {
  38. NSLog(@"select ok.");
  39. sqlite3_bind_text(statement, 1, "张三",-1,NULL);
  40. while (sqlite3_step(statement)==SQLITE_ROW) {
  41. int _id=sqlite3_column_int(statement, 0);
  42. NSString *name=[[NSString alloc] initWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];
  43. NSLog(@"row>>id %i, name %@",_id,name);
  44. }
  45. sqlite3_finalize(statement);
  46. }
  47. sqlite3_close(database);
  48. }

-(void) makeDBinfo{

NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"mydb"];

char *errorMsg;

//打开或创建数据库

sqlite3 *database;

if (sqlite3_open([databaseFilePath UTF8String], &database)!=SQLITE_OK) {

sqlite3_close(database);

}else {

NSLog(@"open sqlite db ok.");

}

//创建数据库表

const char *createSql=" create table if not exists persons (id integer primary key autoincrement,name text)";

if (sqlite3_exec(database, createSql, NULL, NULL, &errorMsg)==SQLITE_OK)

{

NSLog(@"create table ok.");

}else

{

//如果在多个地方使用errorMsg,那么每次使用完毕要清空一下字串,比如这样:

NSLog(@"error: %s",errorMsg);

sqlite3_free(errorMsg);

}

// 向表中插入记录

const char *insertSql="insert into persons (name) values(\"张三\")";

if (sqlite3_exec(database, insertSql, NULL, NULL, &errorMsg)==SQLITE_OK)

{

NSLog(@"insert ok.");

}else

{

//如果在多个地方使用errorMsg,那么每次使用完毕要清空一下字串,比如这样:

NSLog(@"error: %s",errorMsg);

sqlite3_free(errorMsg);

}

//结果集的查询,需要用到statement:

const char *selectSql="select id,name from persons where name = ? ";

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(database, selectSql, -1, &statement, nil)==SQLITE_OK) {

NSLog(@"select ok.");

sqlite3_bind_text(statement, 1, "张三",-1,NULL);

while (sqlite3_step(statement)==SQLITE_ROW) {

int _id=sqlite3_column_int(statement, 0);

NSString *name=[[NSString alloc] initWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];

NSLog(@"row>>id %i, name %@",_id,name);

}

sqlite3_finalize(statement);

}

sqlite3_close(database);

}

删除表操作

constchar *sql_drop_table="drop table if exists t";

constchar *sql_create_table="create table t(id int primary key,msg varchar(128))";

sqlite3_exec(db,sql_drop_table,0,0,&errmsg);

sqlite3_exec(db,sql_create_table,0,0,&errmsg);

插入数据

sqlite3_exec(db,"insert into t(id,msg) values(1,'Ady Liu')",NULL,NULL,&errmsg);

预编译操作

int i = 0;

sqlite3_stmt *stmt;

char ca[255];

//prepare statement

sqlite3_prepare_v2(db,"insert into t(id,msg) values(?,?)",-1,&stmt,0);

for(i=10;i<20;i++){

sprintf(ca,"HELLO#%i",i);

sqlite3_bind_int(stmt,1,i);

sqlite3_bind_text(stmt,2,ca,strlen(ca),NULL);

sqlite3_step(stmt);

sqlite3_reset(stmt);

}

sqlite3_finalize(stmt)

预编译操作比较麻烦的,完整的预编译操作的流程是:
通过sqlite3_prepare_v2()创建一个sqlite3_stmt对象
通过sqlite3_bind_*()绑定预编译字段的值
通过sqlite3_step()执行SQL语句
通过sqlite3_reset()重置预编译语句,重复操作2多次
通过sqlite3_finalize()销毁资源

预编译SQL语句中可以包含如下几种形式:
?
?NNN
:VVV
@VVV
$VVV
NNN代表数字,VVV代表字符串。

如果是?或者?NNN,那么可以直接sqlite3_bind_*()进行操作,如果是字符串,还需要通过sqlite3_bind_parameter_index()获取对应的index,然后再调用sqlite3_bind_*()操作。这通常用于构造不定条件的SQL语句(动态SQL语句)。

查询操作

写道

回调函数的解释参考最上面的描述。 首先声明一个回调函数。
int print_record(void *,int,char **,char **);

查询代码
//select data
ret = sqlite3_exec(db,"select * from t",print_record,NULL,&errmsg);
if(ret != SQLITE_OK){
fprintf(stderr,"query SQL error: %s\n",errmsg);
}

现在定义回调函数,只是简单的输出字段值。
int print_record(void *params,int n_column,char **column_value,char **column_name){
int i;
for(i=0;i<n_column;i++){
printf("\t%s",column_value[i]);
}
printf("\n");
return 0;
}

不使用回调的查询操作

写道

定义使用的变量
char **dbresult; int j,nrow,ncolumn,index;
查询操作
//select table
ret = sqlite3_get_table(db,"select * from t",&dbresult,&nrow,&ncolumn,&errmsg);
if(ret == SQLITE_OK){
printf("query %i records.\n",nrow);
index=ncolumn;
for(i=0;i<nrow;i++){
printf("[%2i]",i);
for(j=0;j<ncolumn;j++){
printf(" %s",dbresult[index]);
index++;
}
printf("\n");
}
}
sqlite3_free_table(dbresult);

受影响的记录数

我们可以使用sqlite3_change(sqlite3 *)的API来统计上一次操作受影响的记录数。
ret = sqlite3_exec(db,"delete from t",NULL,NULL,&errmsg);

if(ret == SQLITE_OK){

printf("delete records: %i\n",sqlite3_changes(db));

}

总结

写道

这里我们接触了SQLITE3的13个API:
sqlite3_open()
sqlite3_exec()
sqlite3_close()
sqlite3_prepare_v2
sqlite3_bind_*()
sqlite3_bind_parameter_index()
sqlite3_step()
sqlite3_reset()
sqlite3_finalize()
sqlite3_get_table
sqlite3_change()
sqlite3_free()
sqlite3_free_table()
事实上截止到SQLITE3.7.14(2012/09/03) 一共提供了204个API函数(http://www.sqlite.org/c3ref/funclist.html)。
但最精简的API函数大概有6个:
sqlite3_open()
sqlite3_prepare()
sqlite3_step()
sqlite3_column()
sqlite3_finalize()
sqlite3_close()
核心API也就10个(在精简API基础上增加4个):
sqlite3_exec()
sqlite3_get_table()
sqlite3_reset()
sqlite3_bind()
因此掌握起来还是比较容易的。