【IOS开发】FMDB的简单使用

【IOS开发】FMDB的简单使用


Demo地址:http://download.csdn.net/detail/u012881779/8442261

//
//  DMDBManage.m
//  DMFMDBDemo

#import "DMDBManage.h"
static FMDatabase *db;
@implementation DMDBManage

/*
 *实例化FMDatabase
 */
+(void)instanceDatabase{
    //Document为ios中可读写的文件夹
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    Nsstring *documentDirectory = [paths objectAtIndex:0];
    
    //dbPath:数据库路径
    Nsstring *dbPath = [documentDirectory stringByAppendingPathComponent:@"DBManage.db"];
    
    //创建数据库实例,如果路径中不存在"DBManage.db"的文件,sqlite会自动创建"DBManage.db"
    db = [FMDatabase databaseWithPath:dbPath] ;
    if (![db open]) {
        
        NSLog(@"Could not open db.");
        return ;
    }else{
    
    }
    //创建表
    [self createTableAction];
}

/*
 *创建表
 *创建一个名为User的表,有两个字段分别为string类型的Name,integer类型的Age
 *[db executeUpdate:@"CREATE TABLE User (Name text,Sex text,Age integer,Native text,Phone integer,Address text)"];
 */
+(void)createTableAction{
    [db executeUpdate:@"CREATE TABLE User (Name text PRIMARY KEY,Address text)"];
}

/*
 *删除表和表中信息
 *将表连同表中信息一起删除但是日志文件中会有记录
 *drop table tabname
 */
+(void)dorpTableAction{
    [db executeUpdate:@"drop table User"];
}

/*
 *删除表中信息
 *将表中信息删除但是会保留这个表
 *delete from tabname
 */
+(void)deleteTableAction{
    [db executeUpdate:@"delete from User"];
}

/*
 *增加一个列
 *注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
 *Alter table tabname add colname coltype
 */
+(void)addColnameAction{
    [db executeUpdate:@"Alter table User add ID coltype"];
}

/*
 *插入数据
 *text对应为Nsstring integer对应为NSNumber的整形
 *[db executeUpdate:@"INSERT INTO User (Name,Sex,Age,Native,Phone,Address) VALUES(?,?,?)",@"姓名",@"男",[NSNumber numberWithInt:20],@"籍贯",@"电话号码",@"地址"];
 */
+(void)insertAction:(NSDictionary *)sqlCode{
    [self instanceDatabase];
    [db executeUpdate:@"INSERT INTO User (Name,[sqlCode objectForKey:@"name"],[sqlCode objectForKey:@"sex"],[NSNumber numberWithInt:[[sqlCode objectForKey:@"age"] integerValue]],[sqlCode objectForKey:@"native"],[sqlCode objectForKey:@"phone"],[sqlCode objectForKey:@"address"]
     ];
}

/*
 *删除数据
 *[db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"张三"];
 */
+(void)removeAction:(NSDictionary *)sqlCode{
    [self instanceDatabase];
    Nsstring *nameStr = [sqlCode objectForKey:@"name"];
    if([self judgeStringIsNull:nameStr]){
        [db executeUpdate:@"DELETE FROM User WHERE Name = ?",nameStr];
    }
}

/*
 *更新数据
 *[db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ?",@"李四",@"张三"];
 */
+(void)updateAction:(NSDictionary *)sqlCode{
    [self instanceDatabase];
    Nsstring *ageStr = [sqlCode objectForKey:@"age"];
    Nsstring *nameStr = [sqlCode objectForKey:@"name"];
    if([self judgeStringIsNull:ageStr] && [self judgeStringIsNull:nameStr]){
        Nsstring *sqlStr = [Nsstring stringWithFormat:@"UPDATE User SET Name = '%@',Sex = '%@',Age = %d,Native = '%@',Phone = '%@',Address = '%@' WHERE Name = '%@'",[ageStr  integerValue],[sqlCode objectForKey:@"address"],nameStr];
      
        [db executeUpdate:sqlStr];
    }
}

/*
 *查找数据
 *
 */
+(NSDictionary *)findAction:(NSDictionary *)sqlCode{
    [self instanceDatabase];
    Nsstring *nameStr = [sqlCode objectForKey:@"name"];
    NSMutableDictionary *sqlDict = [[NSMutableDictionary alloc] init];
    if([self judgeStringIsNull:nameStr]){
        //返回数据库中第一条满足条件的结果
        FMResultSet *rs=[db executeQuery:@"SELECT * FROM User WHERE Name =?",nameStr];
        while ([rs next]){
            [sqlDict setobject:[rs stringForColumn:@"Name"] forKey:@"name"];
            [sqlDict setobject:[rs stringForColumn:@"Sex"]  forKey:@"sex"];
            [sqlDict setobject:[rs stringForColumn:@"Age"]  forKey:@"age"];
            [sqlDict setobject:[rs stringForColumn:@"Native"]  forKey:@"native"];
            [sqlDict setobject:[rs stringForColumn:@"Phone"]   forKey:@"phone"];
            [sqlDict setobject:[rs stringForColumn:@"Address"] forKey:@"address"];
        }
        [rs close];
    }
    return sqlDict;
}

//判断字符串不全为空
+(BOOL)judgeStringIsNull:(Nsstring *)string{
    BOOL result = NO;
    if(string != nil && string.length > 0){
        for (int i = 0; i < string.length; i ++) {
            Nsstring *subStr = [string substringWithRange:NSMakeRange(i,1)];
            if(![subStr isEqualToString:@" "] && ![subStr isEqualToString:@""]){
                result = YES;
            }
        }
    }
    return result;
}

@end


Demo视图:

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能