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"];
原文:https://www.cnblogs.com/pjfeng/p/8981574.html