FMDB 操作数据库

来源:互联网 发布:淘宝充值平台押金 编辑:程序博客网 时间:2024/05/29 17:24
操作数据库,之前,统一操作:
if (![db open]) {
        [db release];
        return;
}
之后统一操作:
[db close];




1、创建表:
[db executeUpdate:[NSString stringWithFormat:@"CREATE TABLE %@ (title text,id text UNIQUE,image text,user text,article text,time text,rt text,text text,weight text,desc text,type text,comments text,reposts text)",tablename]];


2、插入数据:
BOOL isSuccess = [db executeUpdate:[NSString stringWithFormat:@"insert into %@ (title,id,image,user,article,time,rt,text,weight,desc,type,comments,reposts) values(?,?,?,?,?,?,?,?,?,?,?,?,?)",tablename],
                              status.title,
                              status.id_ ,
                              imagedict,
                              userdict,
                              articledict,
                              status.time,
                              rtdict,
                              status.text,
                              status.weight,
                              status.desc,
                              status.type,
                              status.comments,
                              status.reposts
                              ];


3、查找数据:
FMResultSet *rs = [db executeQuery:[NSString stringWithFormat:@"select * from %@ where id = ?",tablename],[statusIdArray objectAtIndex:i]];
while ([rs next]) {
           NSMutableDictionary* statusdict = [[NSMutableDictionary alloc] init];
            if ([rs stringForColumn:@"title"])
                [statusdict setObject:[rs stringForColumn:@"title"] forKey:@"title"];
           。。。。。。
}


4、删除指定表的数据:
NSString *delegeTable = [NSString stringWithFormat:@"delete from %@",[NSString stringWithFormat:@"section_statuses_%@", sectionId]];
[db executeUpdate:delegeTable];


5、删除所有表的数据:
[db executeUpdate:[NSString stringWithFormat:@"delete * from %@",tablename]];


6、查询当前表中的数据条数
[db intForQuery:[NSString stringWithFormat:@"select count(id) from %@",tablename]];

0 0