前言: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语句的编写。(未完待续)