FMDB

FMDB数据库的封装http://www.jianshu.com/p/d329498e00be
iOS数据库之使用FMDB的实际操作http://www.jianshu.com/p/25dfae4f0eeb
关于移动端SQLitehttp://www.jianshu.com/p/4bc34f982fee


#import "FMDB.h"
#import "Model.h"
@interface FMDBManager : NSObject

#pragma mark ----单例----
+(FMDBManager *)shareInstance;

#pragma mark ---打开数据库---
-(void)openDB;

#pragma mark ---在数据库中创建表---
-(void)createTableWithTableName:(NSString *)tableName;

#pragma mark ---添加数据---
-(void)insertDataWithTableName:(NSString *)tableName name:(NSString *)name myID:(NSInteger)myID;

#pragma mark ---删除数据---
-(void)deleteDataWithTableName:(NSString *)tableName  myID:(NSInteger)myID;

#pragma mark ---修改数据---
-(void)updataDataWithTableName:(NSString *)tableName name:(NSString *)name myID:(NSInteger)myID forMyID:(NSInteger)forMyID;
#pragma mark ---查询数据---
-(Model *)selectOneDataWithTableName:(NSString *)tableName myID:(NSInteger)myID;

#pragma mark ---查询所有数据---
-(NSMutableArray *)selectAllDataWithTableName:(NSString *)tableName;
@end

.m

#import "FMDBManager.h"
static FMDatabase *db = nil;

@implementation FMDBManager

#pragma mark ----单例----
+(FMDBManager *)shareInstance
{
    static FMDBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[FMDBManager alloc]init];
        [manager openDB];
        [manager createTableWithTableName:@"chat"];
    });
    return manager;
}

-(NSString *)createSqliteWithSqliteName:(NSString *)sqliteName
{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:sqliteName];
}

#pragma mark ---打开数据库---
-(void)openDB
{
    if (db != nil) {
        return;
    }
    NSString *path = [self createSqliteWithSqliteName:@"lol.sqlite"];
    NSLog(@"path ==== %@",path);
    db = [FMDatabase databaseWithPath:path];
    if ([db open]) {
        NSLog(@"数据库打开成功");
    }
    else {
        NSLog(@"数据库打开失败");
    }
}

#pragma mark ---在数据库中创建表---
-(void)createTableWithTableName:(NSString *)tableName
{
    [db open];
    NSString *string = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, name text, myID integer)",tableName];
    BOOL flag = [db executeUpdate:string];
    NSLog(@"创建表flag ==== %d",flag);
}

#pragma mark ---添加数据---
-(void)insertDataWithTableName:(NSString *)tableName name:(NSString *)name myID:(NSInteger)myID
{
    [db open];
    NSString *string = [NSString stringWithFormat:@"insert into %@ (name, myID) values (?, ?)",tableName];
    NSString *myIDString = [NSString stringWithFormat:@"%ld",(long)myID];
    BOOL flag = [db executeUpdate:string,name,myIDString];
    NSLog(@"插入数据flag ==== %d",flag);
    [db close];
}

#pragma mark ---在特定表名中删除数据---
-(void)deleteDataWithTableName:(NSString *)tableName  myID:(NSInteger)myID
{
    [db open];
    NSString *string = [NSString stringWithFormat:@"delete from %@ where myID = ?",tableName];
    NSString *myIDString = [NSString stringWithFormat:@"%ld",(long)myID];
    BOOL flag = [db executeUpdate:string,myIDString];
    NSLog(@"删除数据flag ==== %d",flag);
    [db close];
}

#pragma mark ---在特定表名中修改数据---
-(void)updataDataWithTableName:(NSString *)tableName name:(NSString *)name myID:(NSInteger)myID forMyID:(NSInteger)forMyID
{
    [db open];
    NSString *string = [NSString stringWithFormat:@"update %@ set name = ?,  myID = ? where myID = ?",tableName];
    NSString *myIDString = [NSString stringWithFormat:@"%ld",(long)myID];
    NSString *forMyIDString = [NSString stringWithFormat:@"%ld",(long)forMyID];
    BOOL flag = [db executeUpdate:string,name,myIDString,forMyIDString];
    NSLog(@"更新数据flag ==== %d",flag);
    [db close];
}

#pragma mark ---查询数据---
-(Model *)selectOneDataWithTableName:(NSString *)tableName myID:(NSInteger)myID
{
    Model *model = [[Model alloc]init];
    [db open];
    NSString *string = [NSString stringWithFormat:@"select * from %@ where myID = ?",tableName];
    NSString *myIDString = [NSString stringWithFormat:@"%ld",(long)myID];
    FMResultSet *result = [db executeQuery:string,myIDString];
    while ([result next]) {
        model.name = [result stringForColumn:@"name"];
        model.myID = [result intForColumn:@"myID"];
    }
    [db close];
    return  model;
}

#pragma mark ---查询所有数据---
-(NSMutableArray *)selectAllDataWithTableName:(NSString *)tableName
{
    NSMutableArray *modelArr = [NSMutableArray array];
    [db open];
    NSString *string = [NSString stringWithFormat:@"select * from %@",tableName];
    FMResultSet *result = [db executeQuery:string];
    while ([result next]) {
        Model *model = [[Model alloc]init];
        model.name = [result stringForColumn:@"name"];
        model.myID = [result intForColumn:@"myID"];
        [modelArr addObject:model];
    }
    [db close];
    return  modelArr;
}

Model.h

#import 

@interface Model : NSObject

@property (nonatomic,strong)NSString * name;
@property (nonatomic , assign) NSInteger myID;
@end

用法

    [[FMDBManager shareInstance]insertDataWithTableName:@"chat" name:@"12311" myID:444];
    [[FMDBManager shareInstance]deleteDataWithTableName:@"chat" myID:0];

   NSMutableArray * arr = [[FMDBManager shareInstance]selectAllDataWithTableName:@"chat"];
    NSLog(@"%@",arr);
    Model * mol = [[Model alloc]init];
    
    mol = [arr objectAtIndex:0];
    
    NSLog(@"%@",mol.name);

你可能感兴趣的:(FMDB)