iOS Fmdb数据存储

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS Fmdb数据存储相关的知识,希望对你有一定的参考价值。

fmdb经常用到,我感觉学习程序的人都会用到数据存储,可是每个语言开发用到的数据库理论是一样的,都是增,删,改,查,ios经常用到fmdb,

1.先实例化一个

#import <Foundation/Foundation.h>

#import "FMDatabase.h"

@interface LNFmdbDate : NSObject

@property (nonatomic ,strong)FMDatabase *db;

@end


#import "LNFmdbDate.h"


@implementation LNFmdbDate


- (instancetype)init

{

    if (self = [super init]) {

        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"OfflineData.sqlite"];

        self.db = [FMDatabase databaseWithPath:path];

    }

    return self;

}


@end

2.继承上面的那个



#import "LNFmdbDate.h"


@interface LNSearchOffine : LNFmdbDate


/**

 *  快速获得实例

 *

 *  @return LNSearchOffine对象

 */

+ (instancetype)shareOffline;


/**

 *  向数据库表中添加名字

 *

 *  @param model 名字详情

 *

 *  @return 插入成功返回YES,失败返回NO

 */

- (BOOL) inserData:(NSDictionary *)model anCity:(NSString *)cityName;


/**

 *  从数据库表中取出所有名字

 *

 *  @return 所有名字信息

 */

- (NSArray*)getAllData:(NSString *)cityName;

/**

 *

 *

 *  @return 判断是否收藏

 */

- (BOOL)isBleanOffineData:(NSString *)name anCity:(NSString *)cityName;



/**

 *  从数据库表中删除指定的名字

 *

 *  @param aName 指定的名字

 *

 *  @return 删除成功返回YES,失败返回NO

 */

- (BOOL) deleteName:(NSString *)aName anCity:(NSString *)cityName;


/**

 *  从数据库表中名字

 *

 *  @return 删除成功返回YES,失败返回NO

 */

- (BOOL) deleteDetail:(NSString *)cityName;


@end


#import "LNSearchOffine.h"


#define COLLECT @"t_Offtin"


@implementation LNSearchOffine


static id _instance;


+ (instancetype)shareOffline;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _instance = [[self alloc] init];

    });

    return _instance;

}


- (instancetype)init

{

    if (self = [super init]) {

        

        // 打开数据库

        if ([self.db open]) {

            NSLog(@"数据库打开成功");

            // 创表

            [self.db executeUpdateWithFormat:@"CREATE TABLE IF NOT EXISTS COLLECT (id integer NOT NULL PRIMARY KEY AUTOINCREMENT,city text,name text,data blob);"];

        } else {

            NSLog(@"数据库打开失败");

        }

    }

    return self;

}


/**

 *  向数据库表中添加名字

 *

 *  @param station 名字详情

 *

 *  @return 插入成功返回YES,失败返回NO

 */

- (BOOL) inserData:(NSDictionary *)model anCity:(NSString *)cityName;

{

    NSString *name = model[@"name"];

    NSArray *resultArry = [self getAllStation:cityName];

    for(NSDictionary *dicm  in resultArry)

    {

        if([name isEqualToString:dicm[@"name"]])

        {

            [self deleteStationWithStationName:name anCity:cityName];

        }

    }

    NSMutableData *data = [[NSMutableData alloc] init];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    [archiver encodeObject:model forKey:@"SomekeyValue"];

    [archiver finishEncoding];

    BOOL result = [self.db executeUpdateWithFormat:@"INSERT INTO COLLECT (city,name,data) VALUES (%@,%@,%@);",cityName,name,data];

    return result;

}


/**

 *  从数据库表中取出所有名字

 *

 *  @return 所有名字信息

 */

- (NSArray*)getAllData:(NSString *)cityName;

{

    NSMutableArray * array = [NSMutableArray array];

    FMResultSet *result = [self.db executeQueryWithFormat:@"SELECT * FROM COLLECT WHERE city =%@;",cityName];

    while (result.next) {

        NSData *data = [result dataForColumn:@"data"];

        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

        NSDictionary *myDictionary = [unarchiver decodeObjectForKey:@"SomekeyValue"];

        [unarchiver finishDecoding];

        [array addObject:myDictionary];

    }

    return array;

}


+ (instancetype)allocWithZone:(struct _NSZone *)zone

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _instance = [super allocWithZone:zone];

    });

    return _instance;

}



- (BOOL)isBleanOffineData:(NSString *)name anCity:(NSString *)cityName;

{

    BOOL result = NO;

    NSArray *arry = [self getAllStation:cityName];

    for(int i = 0 ;i<arry.count; ++i)

    {

        NSDictionary *dic = arry[i];

        NSString *cityName = dic[@"name"];

        if([name isEqualToString:cityName])

        {

            return YES;

        }

    }

    return result;

}


- (BOOL) deleteName:(NSString *)aName anCity:(NSString *)cityName;

    BOOL result = [self.db executeUpdateWithFormat:@"DELETE FROM COLLECT WHERE name =%@ and city = %@;",stationName,cityName];

    return result;

}


- (BOOL) deleteDetail:(NSString *)cityName;

{

    BOOL result = [self.db executeUpdateWithFormat:@"DELETE FROM COLLECT WHERE city =%@;",cityName];

    return result;

}



@end







本文出自 “11204872” 博客,请务必保留此出处http://11214872.blog.51cto.com/11204872/1757260

以上是关于iOS Fmdb数据存储的主要内容,如果未能解决你的问题,请参考以下文章

数据存储_FMDB

IOS_FMDB有关字典数组存储及获取问题

[iOS]数据库第三方框架FMDB详细讲解

IOS数据库FMDB增删改查的使用原创

FMDB之数组字典的存储

iOS 开发之 FMDB 源码分析