sqllite数据库操作帮助类
1、基于框架FMDatabase。
2、连接类
#import <Foundation/Foundation.h>
#import "FMDatabase.h"
@interface DbHelper : NSObject {
FMDatabase *db;
}
- (BOOL)initDatabase;
- (void)closeDatabase;
- (FMDatabase *)getDatabase;
@end
#import "DbHelper.h"
#define DB_NAME @"menu.db"
@implementation DbHelper
- (BOOL)initDatabase
{
BOOL success;
NSError *error;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DB_NAME];
success = [fm fileExistsAtPath:writableDBPath];
if(!success){
NSString *defaultDBPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:DB_NAME];
NSLog(@"%@",defaultDBPath);
success = [fm copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if(!success){
NSLog(@"error: %@", [error localizedDescription]);
}
success = YES;
NSLog(@"Success to open database.");
}
if(success){
db = [[FMDatabase databaseWithPath:writableDBPath] retain];
if ([db open]) {
[db setShouldCacheStatements:YES];
}else{
NSLog(@"Failed to open database.");
success = NO;
}
}
return success;
}
- (void)closeDatabase
{
[db close];
}
- (FMDatabase *)getDatabase
{
if ([self initDatabase]){
return db;
}
return NULL;
}
- (void)dealloc
{
[self closeDatabase];
[db release];
[super dealloc];
}
@end
#import "FMDatabase.h"
@interface DbHelper : NSObject {
FMDatabase *db;
}
- (BOOL)initDatabase;
- (void)closeDatabase;
- (FMDatabase *)getDatabase;
@end
#import "DbHelper.h"
#define DB_NAME @"menu.db"
@implementation DbHelper
- (BOOL)initDatabase
{
BOOL success;
NSError *error;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DB_NAME];
success = [fm fileExistsAtPath:writableDBPath];
if(!success){
NSString *defaultDBPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:DB_NAME];
NSLog(@"%@",defaultDBPath);
success = [fm copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if(!success){
NSLog(@"error: %@", [error localizedDescription]);
}
success = YES;
NSLog(@"Success to open database.");
}
if(success){
db = [[FMDatabase databaseWithPath:writableDBPath] retain];
if ([db open]) {
[db setShouldCacheStatements:YES];
}else{
NSLog(@"Failed to open database.");
success = NO;
}
}
return success;
}
- (void)closeDatabase
{
[db close];
}
- (FMDatabase *)getDatabase
{
if ([self initDatabase]){
return db;
}
return NULL;
}
- (void)dealloc
{
[self closeDatabase];
[db release];
[super dealloc];
}
@end
3、调用
-(BOOL) UpdataData:(NSArray *)list
{
DbHelper *dbhelper=[[DbHelper alloc] init];
FMDatabase *db=[dbhelper getDatabase];
[db beginTransaction];
[db executeUpdate: @"DELETE FROM DishType"];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
[db rocback];
}
for(id model in list)
{
[self insertWithModel:model db:db];
}
[db commit];
[dbhelper release];
}
{
DbHelper *dbhelper=[[DbHelper alloc] init];
FMDatabase *db=[dbhelper getDatabase];
[db beginTransaction];
[db executeUpdate: @"DELETE FROM DishType"];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
[db rocback];
}
for(id model in list)
{
[self insertWithModel:model db:db];
}
[db commit];
[dbhelper release];
}
-(BOOL) insertWithModel:(DishTypeModel *) model db:(FMDatabase *) db
{
[db executeUpdate: @"INSERT INTO DishType(ID,Name,Img,Sequence) VALUES(?,?,?,?)",
model.ID,model.Name,model.Img,model.Sequence];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
[db rockback];
return false;
}
return true;
}