FMDB的简单使用

前言:FMDB的导入和优点直接略过,简单粗暴直接看使用。

1、创建表

    NSString * cinemaSeatMapSql = @"CREATE TABLE IF NOT EXISTS cinemaSeatMap(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,cinemaNo text,hallNo text,seatNo text,seat_col INT,seat_row INT,seat_name text);";
    BOOL isCreate = [_dataBase executeUpdate:cinemaSeatMapSql];
    if (isCreate) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建表失败");
    }
注:我这里创建了一个关于影院影厅座位信息的表,表名cinemaSeat ,括号里面的是这个表中的字段。

2、查询表

NSString * readData = [NSString stringWithFormat:@"select * from cinemaSeatMap where cinemaNo = '%@' and hallNo = '%@';",CinemaNo,hallNo];
    FMResultSet * reset = [_dataBase executeQuery:readData];
    NSMutableArray * listArr = [NSMutableArray array];
    while ([reset next]) {
        BMSeatBtnModel * model = [[BMSeatBtnModel alloc]init];
         model.seat_col = [reset intForColumn:@"seat_col"];
        model.seat_no = [reset stringForColumn:@"seatNo"];
        model.seat_name = [reset stringForColumn:@"seat_name"];
        model.seat_row = [reset intForColumn:@"seat_row"];
        [listArr addObject:model];
    }

注意:”select * from cinemaSeatMap where cinemaNo = ‘%@’ and hallNo = ‘%@’;” 传字符串类型SQL语句要用’ ‘号引上,否则会查询不到相应的结果。

3、插入数据

for (int i = 0; i < tempArr.count; i++) {
        BMSeatBtnModel * model = [tempArr objectAtIndex:i];
        NSString * insertStr = [NSString stringWithFormat:@"insert into cinemaSeatMap(cinemaNo,hallNo,seatNo,seat_col,seat_row,seat_name) values ('%@','%@','%@',%i,%i,'%@');",cinemaNo,hallNo,model.seat_no,model.right_count,model.seat_col,model.seat_row,model.seat_name,model.left_count,model.seat_no_row,model.seat_no_col];
        BOOL isInsert = [_dataBase executeUpdate:insertStr];
        if (isInsert) {
            NSLog(@"插入表成功");
        }else{
            NSLog(@"插入失败");
        }
    }
注意:@"insert into cinemaSeatMap(cinemaNo,hallNo,seatNo,seat_col,seat_row,seat_name) values ('%@','%@','%@',%i,%i,'%@');"
values中的字符串类型(字符串中含有汉字的),要加'' ,否则会报 DB Error: 1 "unrecognized token:@"带汉字的内容";

4、更新表

NSString * updateStr = [NSString stringWithFormat:@"update etagChacheTable set etag = '%@' where hallNo = '%@' and CinemaNo = '%@';",
  BOOL isUpdate = [_dataBase executeUpdate:updateStr];
  if (isUpdate) {
          NSLog(@"数据更新成功");
    }

主要就是数据库的SQL语句的编写。(未完待续)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值