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

FMDB实现增删改查

时间:2016-04-05 19:50:59      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

先用cocoapods安装FMDB,然后在控制器里引入FMDB

  //创建数据库路径
    NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSString *filePath = [doc stringByAppendingPathComponent:@"db.sqlite"];
    
    //创建数据库
    FMDatabase *db = [FMDatabase databaseWithPath:filePath];
    NSLog(@"%@",filePath);
    
    //交互之前,必须要先打开数据库
    if ([db open]) {
        NSLog(@"打开数据库成功");
        //创建表
      BOOL success =  [db executeUpdate:@"CREATE TABLE student (ID INT PRIMARY KEY,name TEXT ,age INT,gender TEXT)"];
        if (success) {
            NSLog(@"创建表成功");
        }
    }
    
#pragma mark -INSERT INTO 插入语句
    //1
  BOOL success = [db executeUpdate:@"INSERT INTO student (ID,name,age,gender)VALUES (?,?,?,?)",@(123),@"张三",@(23),@"男"];
    
    if (success) {
        NSLog(@"插入数据成功");
    }
    
    //2.
    success = [db executeUpdateWithFormat:@"INSERT INTO student(ID,name,age,gender)VALUES(%d,%@,%d,%@)",124,@"张思",22,@"男"];
    if (success) {
        NSLog(@"插入张思成功");
    }
   
    /*
#pragma mark -DELETE FROM 删除数据
    success = [db executeUpdate:@"DELETE FROM student WHERE ID = ?",@(124)];
    if (success) {
        NSLog(@"删除张思成功");
    }
    
    success = [db executeUpdateWithFormat:@"DELETE FROM student WHERE ID = %d",123];
    if (success) {
        NSLog(@"删除张三成功");
    }
    
    
#pragma mark - DROP TABLE销毁表格
    success = [db executeUpdate:@"DROP TABLE IF EXISTS  student"];
    if (success) {
        NSLog(@"销毁表格成功");
    }
     
     */
     
    
#pragma mark - UPDATE SET 修改内容
    success = [db executeUpdate:@"UPDATE student SET  name=? WHERE name = ?",@"李四",@"张三"];
    if (success) {
        NSLog(@"修改张三成功");
    }
    
    success = [db executeUpdateWithFormat:@"UPDATE student SET name= %@ WHERE name= %@",@"王五",@"张思"];
    if (success) {
        NSLog(@"修改张思成功");
    }
    
#pragma mark - SELECT FROM 查询
    FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM student"];
    
    //输出查询结果
    while ([resultSet next]) {
       // NSInteger ID  = [resultSet intForColumn:@"ID"];
        NSInteger ID1 = [resultSet intForColumnIndex:0];
        NSString *name = [resultSet stringForColumnIndex:1];
        NSInteger age = [resultSet intForColumnIndex:2];
        NSString *gender =[resultSet stringForColumnIndex:3];
        NSLog(@"%ld,%@,%ld,%@",ID1,name,age,gender);
        
    }
    
    FMResultSet *resultS = [db executeQuery:@"SELECT * FROM student WHERE ID = ?",@(123)];
    while([resultS next]) {
        
        NSLog(@"%@",[resultS stringForColumnIndex:1]);
    }
    
#pragma mark - FMDB多线程操作
    
    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
    //执行操作
    //错误标识 使用__block
    __block BOOL errorFlag = YES;
    [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        errorFlag &= [db executeUpdate:@"INSERT INTO student (ID,name,age,gender)VALUES(?,?,?,?)",@(125),@"赵四",@(22),@"女"];
        
    }];
    //是否出错
    if (errorFlag) {
        NSLog(@"执行成功,没有出错");
    }
    
}

FMDB实现增删改查

标签:

原文地址:http://www.cnblogs.com/lidongtian/p/5356370.html

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