SQLite简单运用

2016-03-04  本文已影响0人  FeelYoung
#import <Foundation/Foundation.h>

@interface DBHelper : NSObject

@property (nonatomic) BOOL isDBOpen;

+ (instancetype)getInstance;
- (int)execNoQueryWithSQL:(NSString*)sql;
- (NSMutableArray*)execQueryWithSQL:(NSString*)sql;

@end

我们只实现一个“数据库是否打开”的属性,一个获取实例的类方法,一个执行查询语句的成员方法,一个执行非查询语句的成员方法。

static NSArray* docPath;
static NSString* dbPath;
static sqlite3 *db;
static DBHelper* dbhelper;

在类方法中,实现获取dbhelper:

+ (instancetype)getInstance {
    if (dbhelper == nil) {
        dbhelper = [[DBHelper alloc]init];
    }
    return dbhelper;
}

- (id)init {
    self = [super init];
    if ([self class] == [DBHelper class]) {
        docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        dbPath = [[docPath objectAtIndex:0]stringByAppendingPathComponent:@"guessCityDB.sqlite"];
    }
    return self;
}
- (void)open {
    _isDBOpen = sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK;
}

- (void)close {
    sqlite3_close(db);
    _isDBOpen = NO;
}
- (int)execNoQueryWithSQL:(NSString*)sql {
    [self open];
    if (self.isDBOpen) {
        const char* execSQL = [sql UTF8String];
        char *error = NULL;
        int result = -1;
        if (sqlite3_exec(db, execSQL, NULL, NULL, &error) == SQLITE_OK) {
            result = sqlite3_changes(db);
        }
        if (error != NULL)
        {
            sqlite3_free(error);
        }
        [self close];
        return result;
    } else {
        [self close];
        return -1;
    }
}

执行失败返回-1,执行成功返回“执行SQL后改变的数据数量”。

- (NSMutableArray*)execQueryWithSQL:(NSString*)sql {
    [self open];
    if (self.isDBOpen) {
        NSMutableArray* result = [NSMutableArray arrayWithCapacity:100];
        const char* execSQL = [sql UTF8String];
        sqlite3_stmt* statement = NULL;
        if (sqlite3_prepare(db, execSQL, -1, &statement, NULL) == SQLITE_OK) {
            while(sqlite3_step(statement) == SQLITE_ROW) {
                int colNum = sqlite3_column_count(statement);
                NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:colNum];
                for (int i=0; i<colNum; i++) {
                    const char* colName = sqlite3_column_name(statement, i);
                    const char* value = (const char*)sqlite3_column_text(statement, i);
                    if (value != NULL) {
                        [dic setObject:[NSString stringWithUTF8String:value] forKey:[[NSString stringWithUTF8String:colName]uppercaseString]];
                    } else {
                        [dic setObject:@"" forKey:[[NSString stringWithUTF8String:colName]uppercaseString]];
                    }
                }
                [result addObject:dic];
            }
            sqlite3_finalize(statement);
            [self close];
            return result;
        } else {
            [self close];
            return nil;
        }
    } else {
        [self close];
        return nil;
    }
}
#import "CityDAO.h"
#import "DBHelper.h"

@implementation CityDAO

- (NSMutableArray<CityVO*>*)getCityDB {
    NSString* sql = @"select * from city";
    NSMutableArray* result = [[DBHelper getInstance] execQueryWithSQL:sql];
    NSMutableArray* cityVOs = [NSMutableArray arrayWithCapacity:result.count];
    for (int i=0; i<result.count; i++) {
        NSMutableDictionary* dic = [result objectAtIndex:i];
        [cityVOs addObject:[self translateCityVOByNSMutableDictionary:dic]];
    }
    return cityVOs;
}

- (CityVO*)getCityByID:(int)cityid {
    NSString* sql = [NSString stringWithFormat:@"select * from city where cityid=%d", cityid];
    NSMutableArray* result = [[DBHelper getInstance] execQueryWithSQL:sql];
    if (result.count == 1) {
        NSMutableDictionary* dic = [result objectAtIndex:0];
        return [self translateCityVOByNSMutableDictionary:dic];
    } else {
        return nil;
    }
}

- (UIImage*)getIamgeFromCity:(CityVO*)cityVO {
    return [UIImage imageNamed:cityVO.imageName];
}

- (CityVO*)translateCityVOByNSMutableDictionary:(NSMutableDictionary*)dic {
    int cityid = [(NSString*)[dic objectForKey:@"CITYID"] intValue];
    NSString* cityname = (NSString*)[dic objectForKey:@"NAME"];
    NSString* simplename = (NSString*)[dic objectForKey:@"SIMPLENAME"];
    NSString* imagename = (NSString*)[dic objectForKey:@"IMAGENAME"];
    return [[CityVO alloc]initWithID:cityid WithName:cityname WithSimpleName:simplename WithImageName:imagename];
}

@end
上一篇 下一篇

猜你喜欢

热点阅读