首页 > 数据库技术 > 详细

FMDB数据库使用

时间:2018-05-02 18:41:15      阅读:193      评论:0      收藏:0      [点我收藏+]

创建数据库路径

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数据库使用

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!