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

FMDB数据库使用

时间:2018-05-02 19:11:44      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:cto   key   rdo   note   count   panel   nss   obj   sqli   

创建数据库路径

NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString* dbpath = [docsdir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite",sqlName]];

注:sqlName  数据库名称

//创建数据库

FMDatabase *dataBase = [FMDatabase databaseWithPath:dbPath];

if ([dataBase open]) {  //打开数据库

NSString *sqlStr = [NSString stringWithFormat:@"CREATE TABLE ‘%@‘ (",tableName];

注:tableName  创建表名      keyAll  表中字段的数组集合

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

sqlStr = [sqlStr stringByAppendingString:[NSString stringWithFormat:@"‘%@‘ VARCHAR(30)",keyAll[i]]];

if (i == keyAll.count-1) {

sqlStr = [sqlStr stringByAppendingString:@")"];

}else{

sqlStr = [sqlStr stringByAppendingString:@","];

}

}

BOOL res = [dataBase executeUpdate:sqlStr];

if (!res) {

NSLog(@"error when creating db table");

}else{

NSLog(@"succ to creating db table");

}

[dataBase close];

}

插入数据

NSString *sqlStr = @"INSERT INTO    tableName (name , age)values(? , ?)"

[db executeUpdate:sqlStr,@"小明", @"17"];

修改数据

NSString *uadataSql = [NSString stringWithFormat:@"update %@ set age = (?) where name = ‘小明‘",tableName];

[dataBase executeUpdate:uadataSql,@"12"];

 

FMDB数据库使用

标签:cto   key   rdo   note   count   panel   nss   obj   sqli   

原文地址:https://www.cnblogs.com/pjfeng/p/8981574.html

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