博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

iPhone开发 – 数据持久化

Posted on 2011-05-16 18:03  BradyChen  阅读(1147)  评论(0编辑  收藏  举报

转自:http://www.jguoer.com/blog/index.php/archives/171

在开发应用程序的时候,当然需要经常的实用数据库进行数据的保存了,在移动设备上,我们可以使用文件,数据库等方式去保存,为了能够让用户无法使用其他的程序去修改,我这里认为使用数据库的方式是一个很好的方式。在iPhone上面,我们可以使用SQLite进行数据的持久化。另外值得一提的是Firefox是使用数据库的方式保存的,同样也是SQLite。

在iPhone开发重,我们需要首先添加一个SQLite的库,XCode本身就支持的,我们在左边的Frameworks里面选择Add,然后选择Existing Frameworks,在弹出窗口中选择SQLite的库libsqlite3.0.dylib。

添加之后,我们就可以使用SQLite在iPhone中进行数据的保存,查询,删除等操作了。

现在我们可以写一个SQLite的Helper文件,方便我们在其他的代码中使用,头文件(SqliteHelper.h)如下。

#import <Foundation/Foundation.h>
#import sqlite3.h
#define kFileName @”mydatabase.sql” 

@interface SqliteHelper : NSObject {

sqlite3 *database; 
}

//创建表
- (BOOL)createTable;
//插入数据
- (BOOL)insertMainTable:(NSString*) username insertPassword:(NSString*) password;
//查询表
- (BOOL)checkIfHasUser;
 

@end

我们的代码文件如下。

#import SqliteHelper.h

@implementation SqliteHelper

- (BOOL)createTable
{
NSArray 
*path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString 
*paths = [[path objectAtIndex:0] stringByAppendingPathComponent:kFileName];
NSFileManager 
*fileManager = [NSFileManager defaultManager];
BOOL fileFinded 
= [fileManager fileExistsAtPath:paths];

NSLog(@”Database file path is %@,paths);

if(fileFinded)
{
NSLog(
@”Database file existed);

if(sqlite3_open([paths UTF8String],&database)!=SQLITE_OK)
{
sqlite3_close(database);
NSLog(
@”Open Failed);
return NO;
}
}
else
{
NSLog(
@”Database file is not existed);

if(sqlite3_open([paths UTF8String],&database)!=SQLITE_OK)
{
sqlite3_close(database);
NSLog(
@”Open Failed);
return NO;
}
}

char *errorMsg;

NSString *createSQL = @”create table if not exists fields (userid integer primary key,username text,password text);
if(sqlite3_exec(database,[createSQL UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)
{
sqlite3_close(database);
NSLog(
@”Open failed or init filed);
return NO;
}

return YES;
}

- (BOOL)insertMainTable:(NSString*) username insertPassword:(NSString*) password
{
char *errorMsg;

NSString *createSQL = @”create table if not exists fields (userid integer primary key,username text,password text);
if(sqlite3_exec(database,[createSQL UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)
{
sqlite3_close(database);
NSLog(
@”Open failed or init filed);
return NO;
}

NSString *insertData = [[NSString alloc] initWithFormat:@”insert or replace into fields (userid,username,password) values (%d,’%@’,'%@’),0,username,password];

if(sqlite3_exec(database,[insertData UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)
{
sqlite3_close(database);
NSLog(
@”Open failed or failed to insert);
return NO;
}

return YES;
}

- (BOOL)checkIfHasUser
{
NSString 
*getUserCountSQL = @”select * from fields;
sqlite3_stmt 
*statement;

NSLog(@”checkIfHasUser);

if(sqlite3_prepare_v2(database,[getUserCountSQL 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,2);
// NSString *fieldName = [[NSString alloc] initWithFormat:@”show%d”,row];
// NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];
//
// NSLog(@”fieldName is :%@,fieldValue is :%@”,fieldName,fieldValue);
// return [[NSString alloc] initWithFormat:@”fieldName is :%@,fieldValue is :%@”,fieldName,fieldValue];
//
// [fieldName release];
// [fieldValue release];
//}
//sqlite3_finalize(statement);

if(sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(
@”Have user);
return YES;
}
}

NSLog(@”No user);
return NO;
}

@end

其中checkIfHasUser是检查数据,这个方法中我注释的是得到数据,因为我们这里只是check,所以不需要得到数据,直接看是否存在数据即可。上面的代码虽然没有过多的注释,但是代码本身已经很简单了,上下文也非常清楚,所以我就不写过多的注释了。