标签:
闲来无事写点东西,希望大家多多指正!
大家先去git下载FMDB,然后将其中source文件夹中的fmdb文件夹拖入自己的项目中。最后就可以引用下面的代码对fmdb进行一次简单的封装。
这样可以更直观的使用数据库进行增删改操作。
/**
* 可以存储数据类型 text integer blob boolean date
* keyTypes 存储的字段 以及对应数据类型
* keyValues 存储的字段 以及对应的值
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "FMDB.h"
@interface YZKDBTool : NSObject
/**
* 数据库工具单例
*
* @return 数据库工具对象
*/
+(YZKDBTool *)tool;
/**
* 创建数据库
*
* @param dbName 数据库名称(带后缀.sqlite)
*/
-(FMDatabase *)getDBWithDBName:(NSString *)dbName;
/**
* 给指定数据库建表
*
* @param db 指定数据库对象
* @param tableName 表的名称
* @param keyTypes 所含字段以及对应字段类型 字典
*/
-(void)DataBase:(FMDatabase *)db createTable:(NSString *)tableName keyTypes:(NSDictionary *)keyTypes;
/**
* 给指定数据库的表添加值
*
* @param db 数据库名称
* @param keyValues 字段及对应的值
* @param tableName 表名
*/
-(void)DataBase:(FMDatabase *)db insertKeyValues:(NSDictionary *)keyValues intoTable:(NSString *)tableName;
/**
* 给指定数据库的表更新值
*
* @param db 数据库名称
* @param keyValues 要更新字段及对应的值
* @param tableName 表名
*/
-(void)DataBase:(FMDatabase *)db updateTable:(NSString *)tableName setKeyValues:(NSDictionary *)keyValues;
/**
* 条件更新
*
* @param db 数据库名称
* @param tableName 表名称
* @param keyValues 要更新的字段及对应值
* @param condition 条件
*/
-(void)DataBase:(FMDatabase *)db updateTable:(NSString *)tableName setKeyValues:(NSDictionary *)keyValues whereCondition:(NSDictionary *)condition;
/**
* 查询数据库表中的所有值 限制数据条数10
*
* @param db 数据库名称
* @param keysTypes 查询字段以及对应字段类型 字典
* @param tableName 表名称
* @return 查询得到数据
*/
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName;
/**
* 条件查询数据库中的数据 限制数据条数10
*
* @param db 数据库名称
* @param keysTypes 查询字段以及对应字段类型 字典
* @param tableName 表名称
* @param condition 条件
*
* @return 查询得到数据 限制数据条数10
*/
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereCondition:(NSDictionary *)condition;
/**
* 模糊查询 某字段以指定字符串开头的数据 限制数据条数10
*
* @param db 数据库名称
* @param keysTypes 查询字段以及对应字段类型 字典
* @param tableName 表名称
* @param key 条件字段
* @param str 开头字符串
*
* @return 查询所得数据 限制数据条数10
*/
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereKey:(NSString *)key beginWithStr:(NSString *)str;
/**
* 模糊查询 某字段包含指定字符串的数据 限制数据条数10
*
* @param db 数据库名称
* @param keysTypes 查询字段以及对应字段类型 字典
* @param tableName 表名称
* @param key 条件字段
* @param str 所包含的字符串
*
* @return 查询所得数据
*/
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereKey:(NSString *)key containStr:(NSString *)str;
/**
* 模糊查询 某字段以指定字符串结尾的数据 限制数据条数10
*
* @param db 数据库名称
* @param keysTypes 查询字段以及对应字段类型 字典
* @param tableName 表名称
* @param key 条件字段
* @param str 结尾字符串
*
* @return 查询所得数据
*/
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereKey:(NSString *)key endWithStr:(NSString *)str;
/**
* 清理指定数据库中的数据 (只删除数据不删除数据库)
*
* @param db 指定数据库
*/
-(void)clearDatabase:(FMDatabase *)db from:(NSString *)tableName;
@end
#import "YZKDBTool.h"
static YZKDBTool *tool = nil;
@implementation YZKDBTool
+(YZKDBTool *)tool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (tool == nil) {
tool = [[self alloc] init];
}
});
return tool;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (tool == nil) {
tool = [super allocWithZone:zone];
}
});
return tool;
}
#pragma mark --创建数据库
-(FMDatabase *)getDBWithDBName:(NSString *)dbName
{
NSArray *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *dbPath = [library[0] stringByAppendingPathComponent:dbName];
NSLog(@"%@", dbPath);
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
if (![db open]) {
NSLog(@"无法获取数据库");
return nil;
}
return db;
}
#pragma mark --给指定数据库建表
-(void)DataBase:(FMDatabase *)db createTable:(NSString *)tableName keyTypes:(NSDictionary *)keyTypes
{
if ([self isOpenDatabese:db]) {
NSMutableString *sql = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (",tableName]];
int count = 0;
for (NSString *key in keyTypes) {
count++;
[sql appendString:key];
[sql appendString:@" "];
[sql appendString:[keyTypes valueForKey:key]];
if (count != [keyTypes count]) {
[sql appendString:@", "];
}
}
[sql appendString:@")"];
// NSLog(@"%@", sql);
[db executeUpdate:sql];
}
}
#pragma mark --给指定数据库的表添加值
-(void)DataBase:(FMDatabase *)db insertKeyValues:(NSDictionary *)keyValues intoTable:(NSString *)tableName
{
if ([self isOpenDatabese:db]) {
// int count = 0;
// NSString *Key = [[NSString alloc] init];
// for (NSString *key in keyValues) {
// if(count == 0){
// NSMutableString *sql = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (?)",tableName, key]];
// [db executeUpdate:sql,[keyValues valueForKey:key]];
// Key = key;
// }else
// {
// NSMutableString *sql = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"UPDATE %@ SET %@ = ? WHERE %@ = ?", tableName, key, Key]];
// [db executeUpdate:sql,[keyValues valueForKey:key],[keyValues valueForKey:Key]];
// }
// count++;
// }
NSArray *keys = [keyValues allKeys];
NSArray *values = [keyValues allValues];
NSMutableString *sql = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"INSERT INTO %@ (", tableName]];
NSInteger count = 0;
for (NSString *key in keys) {
[sql appendString:key];
count ++;
if (count < [keys count]) {
[sql appendString:@", "];
}
}
[sql appendString:@") VALUES ("];
for (int i = 0; i < [values count]; i++) {
[sql appendString:@"?"];
if (i < [values count] - 1) {
[sql appendString:@","];
}
}
[sql appendString:@")"];
NSLog(@"%@", sql);
[db executeUpdate:sql withArgumentsInArray:values];
}
}
#pragma mark --给指定数据库的表更新值
-(void)DataBase:(FMDatabase *)db updateTable:(NSString *)tableName setKeyValues:(NSDictionary *)keyValues
{
if ([self isOpenDatabese:db]) {
for (NSString *key in keyValues) {
NSMutableString *sql = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"UPDATE %@ SET %@ = ?", tableName, key]];
[db executeUpdate:sql,[keyValues valueForKey:key]];
}
}
}
#pragma mark --条件更新
-(void)DataBase:(FMDatabase *)db updateTable:(NSString *)tableName setKeyValues:(NSDictionary *)keyValues whereCondition:(NSDictionary *)condition
{
if ([self isOpenDatabese:db]) {
for (NSString *key in keyValues) {
NSMutableString *sql = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"UPDATE %@ SET %@ = ? WHERE %@ = ?", tableName, key, [condition allKeys][0]]];
[db executeUpdate:sql,[keyValues valueForKey:key],[keyValues valueForKey:[condition allKeys][0]]];
}
}
}
#pragma mark --查询数据库表中的所有值
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName
{
FMResultSet *result = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ LIMIT 10",tableName]];
return [self getArrWithFMResultSet:result keyTypes:keyTypes];
}
#pragma mark --条件查询数据库中的数据
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereCondition:(NSDictionary *)condition;
{
if ([self isOpenDatabese:db]) {
FMResultSet *result = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ? LIMIT 10",tableName, [condition allKeys][0]], [condition valueForKey:[condition allKeys][0]]];
return [self getArrWithFMResultSet:result keyTypes:keyTypes];
}else
return nil;
}
#pragma mark --模糊查询 某字段以指定字符串开头的数据
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereKey:(NSString *)key beginWithStr:(NSString *)str
{
if ([self isOpenDatabese:db]) {
FMResultSet *result = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE %@%% LIMIT 10",tableName, key, str]];
return [self getArrWithFMResultSet:result keyTypes:keyTypes];
}else
return nil;
}
#pragma mark --模糊查询 某字段包含指定字符串的数据
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereKey:(NSString *)key containStr:(NSString *)str
{
if ([self isOpenDatabese:db]) {
FMResultSet *result = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE %%%@%% LIMIT 10",tableName, key, str]];
return [self getArrWithFMResultSet:result keyTypes:keyTypes];
}else
return nil;
}
#pragma mark --模糊查询 某字段以指定字符串结尾的数据
-(NSArray *)DataBase:(FMDatabase *)db selectKeyTypes:(NSDictionary *)keyTypes fromTable:(NSString *)tableName whereKey:(NSString *)key endWithStr:(NSString *)str
{
if ([self isOpenDatabese:db]) {
FMResultSet *result = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE %%%@ LIMIT 10",tableName, key, str]];
return [self getArrWithFMResultSet:result keyTypes:keyTypes];
}else
return nil;
}
#pragma mark --清理指定数据库中的数据
-(void)clearDatabase:(FMDatabase *)db from:(NSString *)tableName
{
if ([self isOpenDatabese:db]) {
[db executeUpdate:[NSString stringWithFormat:@"DELETE FROM %@",tableName]];
}
}
#pragma mark --CommonMethod
-(NSArray *)getArrWithFMResultSet:(FMResultSet *)result keyTypes:(NSDictionary *)keyTypes
{
NSMutableArray *tempArr = [NSMutableArray array];
while ([result next]) {
NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
for (int i = 0; i < keyTypes.count; i++) {
NSString *key = [keyTypes allKeys][i];
NSString *value = [keyTypes valueForKey:key];
if ([value isEqualToString:@"text"]) {
// 字符串
[tempDic setValue:[result stringForColumn:key] forKey:key];
}else if([value isEqualToString:@"blob"])
{
// 二进制对象
[tempDic setValue:[result dataForColumn:key] forKey:key];
}else if ([value isEqualToString:@"integer"])
{
// 带符号整数类型
[tempDic setValue:[NSNumber numberWithInt:[result intForColumn:key]]forKey:key];
}else if ([value isEqualToString:@"boolean"])
{
// BOOL型
[tempDic setValue:[NSNumber numberWithBool:[result boolForColumn:key]] forKey:key];
}else if ([value isEqualToString:@"date"])
{
// date
[tempDic setValue:[result dateForColumn:key] forKey:key];
}
}
[tempArr addObject:tempDic];
}
return tempArr;
}
-(BOOL)isOpenDatabese:(FMDatabase *)db
{
if (![db open]) {
[db open];
}
return YES;
}
@end
标签:
原文地址:http://www.cnblogs.com/sxwangjiadong/p/4968536.html