码迷,mamicode.com
首页 > 数据库 > 详细

fmdb常用操作

时间:2015-04-22 14:09:35      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

-(NSString *)databaseFilePath
{
    //获取数据库路经
    NSString *url = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [url stringByAppendingPathComponent:@"sqlTJL.sqlite"];
    return fileName;
}

-(void)fmdbData
{
    
    //获取数据库
    _db = [FMDatabase databaseWithPath:[self databaseFilePath]];
    //打开数据库
    if ([_db open]) {
    //建表
        BOOL result = [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS TJL_student(name text)"];
        if (result) {
            NSLog(@"建表成功");
        }else{
            NSLog(@"建表失败");
        }
        [_db close];
    }
}

//插入数据
-(void)insetsqlto:(NSString *)string
{
    [_db open];
    if ([_db open]) {
        BOOL res = [_db executeUpdate:@"insert into TJL_student (name) VALUES(?)", string];
        if (!res) {
            NSLog(@"error");
        }else{
            NSLog(@"success to insert");
        }
        [_db close];
    }
}
//删除数据
-(void)deleteopen:(NSString *)dataName
{
    if ([_db open]) {
        NSString *deleteSql = [NSString stringWithFormat:@"delete from TJL_student %@",dataName];
        BOOL res = [_db executeUpdate:deleteSql];
        if (!res) {
            NSLog(@"error when delete db table");
        }else{
            NSLog(@"success to delete db table");
        }
        [_db open];
    }
}

//修改数据
-(void)updataName:(NSString *)string
{
    if ([_db open]) {
        NSString *updatesql = [NSString stringWithFormat:@"UPDATE TJL_student‘%@‘",string];
        BOOL RES = [_db executeUpdate:updatesql];
        if (!RES) {
            NSLog(@"error when update db table");
        }else{
            NSLog(@"success to insert db able");
        }
        [_db close];
    }
}

//查询数据
-(void)seacher:(NSString *)seaharName
{
    if ([_db open]) {
        FMResultSet *rs = [_db executeQuery:@"SELECT * FROM TJL_student"];
        while ([rs next]) {
            _Devices = [rs stringForColumn:@"name"];
            NSLog(@"is text---->>> %@",[rs stringForColumn:@"name"]);
        }
    }
}


fmdb常用操作

标签:

原文地址:http://my.oschina.net/jilin/blog/404962

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!