在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中。symbian中因为权限认证的原因,在3rd上大多数只能访问应用程序的private目录或其它系统共享目录。在iphone中,apple博采众长,提供了多种数据持久化的方法,下面笔者会逐个进行详细的讲解。
iphone提供的数据持久化的方法,从数据保存的方式上讲可以分为三大部分:属性列表、对象归档、嵌入式数据库(sqlite3)、其他方法。
一、属性列表NSUserDefaults
NSUserDefaults类的使用和NSKeyedArchiver有很多类似之处,但是查看NSUserDefaults的定义可以看出,NSUserDefaults直接继承自NSObject而NSKeyedArchiver 继承自NSCoder。这意味着NSKeyedArchiver实际上是个归档持久化的类,也就可以使用NSCoder类的[encodeObject: (id)objv forKey:(NSString *)key]方法来对数据进行持久化存储。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSMutableArray *persistentArray = [[NSMutableArray alloc] init];
[persistentArray addObject:strOne];
[persistentArray addObject:strTwo];
//archive
NSUserDefaults *persistentDefaults = [NSUserDefaults standardUserDefaults];
[persistentDefaults setObject:persistentArray forKey:@"myDefault"];
NSString *descriptionDefault = [persistentDefaults description];
NSLog(@"NSUserDefaults description is :%@",descriptionDefault);
//unarchive
NSArray *UnpersistentArray =
[persistentDefaults objectForKey:@"myDefault"];
NSString *UnstrOne = [UnpersistentArray objectAtIndex:0];
NSString *UnstrTwo = [UnpersistentArray objectAtIndex:1];
NSLog(@"UnstrOne = %@,UnstrTwo = %@",UnstrTwo);
// Override point for customization after application launch
[window makeKeyAndVisible];
}
二、对象归档NSKeyedArchiver和NSKeyedUnarchiver
iPhone和symbian 3rd一样,会为每一个应用程序生成一个私有目录,这个目录位于
/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次的,上一次的应用程序目录信息被转换成名为.DS_Store隐藏文件,这个目录的文件结构如下图:
通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)得到,代码如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSArray *persistentArray = [NSArray arrayWithObjects:strOne,strTwo,nil];
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSAllDomainsMask,YES);
int pathLen = [pathArray count];
NSLog(@"path number is :%d",pathLen);
NSString *filePath;
for(int i = 0; i < pathLen; i++)
{
filePath = [pathArray objectAtIndex:i];
NSLog(@"%d path is :%@",i,filePath);
}
NSString *myFilename = [filePath stringByAppendingPathComponent:@"myFile.rtf"];
NSLog(@"myfile's path is :%@",myFilename);
// no files generated in correspond directory now
[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename];
// now the myFile.rtf is generated
// Override point for customization after application launch
[window makeKeyAndVisible];
}
NSSearchPathForDirectoriesInDomains()的第二个参数是个枚举值,在笔者的测试代码中,只有NSUserDomainMask和NSAllDomainsMask可以获取到目录数为1,其余的皆为0,打印出来的结果如下:
[Session started at 2009-11-10 21:30:08 +0800.]
2009-11-10 21:30:10.516 PersistentExample[763:207] path number is :1
2009-11-10 21:30:10.518 PersistentExample[763:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents
2009-11-10 21:30:10.521 PersistentExample[763:207] myfile's path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf
Terminating in response to SpringBoard's termination.
[Session started at 2009-11-10 21:32:27 +0800.]
2009-11-10 21:32:30.091 PersistentExample[803:207] path number is :1
2009-11-10 21:32:30.092 PersistentExample[803:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents
2009-11-10 21:32:30.100 PersistentExample[803:207] myfile's path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf
Terminating in response to SpringBoard's termination.
从打印的结果如下,每次应用程序启动时生成的数字字母串目录名字并不一样。在调用[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename]方法前,文件myFile.rtf并每生成,只有在调用此方法后才产生相应的文件。
下面需要把数据从属性列表中读取出来,在上面的代码中,笔者使用NSArray保存数据。但在大多数应用程序中,数据的尺寸并不是固定的,这个时候就需要使用NSMutalbeArray动态的保存数据,代码优化如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *myFilename;
// archive
{
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSMutableArray *persistentArray = [[NSMutableArray alloc] init];
[persistentArray addObject:strOne];
[persistentArray addObject:strTwo];
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
int pathLen = [pathArray count];
NSLog(@"path number is :%d",pathLen);
NSString *filePath;
for(int i = 0; i < pathLen; i++)
{
filePath = [pathArray objectAtIndex:i];
NSLog(@"%d path is :%@",filePath);
}
myFilename = [filePath stringByAppendingPathComponent:@"myFile.rtf"];
NSLog(@"myfile's path is :%@",myFilename);
[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename];
}
// unarchive
{
NSArray *unarchiveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:myFilename];
NSString *UnstrOne = [unarchiveArray objectAtIndex:0];
NSString *UnstrTwo = [unarchiveArray objectAtIndex:1];
NSLog(@"UnstrOne = #define kFileName @"mydb.sql"
@interface PersistentExampleAppDelegate : NSObject <UIApplicationDelegate> {
sqlite3 *database;
UIWindow *window;
}
@property (nonatomic,retain) IBOutlet UIWindow *window;
@end
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *paths = [[path objectAtIndex:0] stringByAppendingPathComponent:kFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL findFile = [fileManager fileExistsAtPath:paths];
NSLog(@"Database file path = %@",paths);
// 如果找到了数据库文件
if(findFile)
{
NSLog(@"Database file have already existed.");
if(sqlite3_open([paths UTF8String],&database) != sqlITE_OK)//打开数据库失败
{
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
}
}else
{
NSLog(@"Database file does not exsit!");
if(sqlite3_open([paths UTF8String],@"Failed to open database");
}
}
char *errorMsg;
//创建表
NSString *createsql = @"create table if not exists fields (row integer primary key,field_data text);";
if(sqlite3_exec(database,[createsql UTF8String],NULL,&errorMsg)!=sqlITE_OK)
{
sqlite3_close(database);
NSAssert1(0,@"Error creating table: %s",errorMsg);
}
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSMutableArray *persistentArray = [[NSMutableArray alloc] init];
[persistentArray addObject:strOne];
[persistentArray addObject:strTwo];
for (int i = 0; i < [persistentArray count]; i++) {
NSString *upDatasql = [[NSString alloc] initWithFormat:@"insert or replace into
fields (row,field_data) values (%d,'%@');",[persistentArray objectAtIndex:i]];
char* errorMsg;
if(sqlite3_exec(database,[upDatasql UTF8String],&errorMsg)
!= sqlITE_OK)
{
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
}
}
//unarchive
NSString *query = @"select row,field_data from fields order by row";//查找表中的数据
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database,[query UTF8String],-1,&statement,nil)
== sqlITE_OK)
{
while(sqlite3_step(statement) == sqlITE_ROW)
{
int row = sqlite3_column_int(statement,0);
char *rowData = (char *)sqlite3_column_text(statement,1);
NSString *fieldName = [[NSString alloc] initWithFormat:@"show%d",row];
NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];
NSLog(@"fieldName is :%@,fieldValue is :%@",fieldName,fieldValue);
[fieldName release];
[fieldValue release];
}
sqlite3_finalize(statement);
}
// Override point for customization after application launch
[window makeKeyAndVisible];
}
在上面的代码中,我们使用
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL findFile = [fileManager fileExistsAtPath:paths];
来判断数据库文件是否已经存在,其实在大多数情况下是没有必要的,sqlite3_open()方法会自动帮我们判断数据库文件是否存在,如果不存在则创建心的数据库文件。
四、其它方法
除了上面的三种方法来保存持久化数据以外,我们还可以用写文件到磁盘的方式来保存持久化数据。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSMutableArray *persistentArray = [[NSMutableArray alloc] init];
[persistentArray addObject:strOne];
[persistentArray addObject:strTwo];
NSArray *filePathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
NSString *filePath =
- [[filePathArray objectAtIndex:0] stringByAppendingPathComponent:@"mydatas.plist"];
[[NSArray arrayWithObjects:persistentArray,nil] writeToFile:filePath atomically:NO];
//load
NSMutableArray *saveDataArray = [[NSMutableArray alloc] init];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
saveDataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
else
saveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle
- mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];
-
NSArray *strArray = [saveDataArray objectAtIndex:0];
NSString *UnstrOne = [strArray objectAtIndex:0];
NSString *UnstrTwo = [strArray objectAtIndex:1];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dongfengsun/archive/2009/11/11/4799249.aspx
原文链接:https://www.f2er.com/sqlite/202896.html