前言:FMDB的導(dǎo)入和優(yōu)點(diǎn)直接略過泣棋,簡單粗暴直接看使用隘道。
1赌渣、創(chuàng)建表
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(@"創(chuàng)建表成功");
}else{
NSLog(@"創(chuàng)建表失敗");
}
注:我這里創(chuàng)建了一個(gè)關(guān)于影院影廳座位信息的表,表名cinemaSeat 淮腾,括號里面的是這個(gè)表中的字段糟需。
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語句要用' '號引上,否則會(huì)查詢不到相應(yīng)的結(jié)果洲押。
3武花、插入數(shù)據(jù)
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中的字符串類型(字符串中含有漢字的),要加'' 杈帐,否則會(huì)報(bào) DB Error: 1 "unrecognized token:@"帶漢字的內(nèi)容";
4体箕、更新表
NSString * updateStr = [NSString stringWithFormat:@"update etagChacheTable set etag = '%@' where hallNo = '%@' and CinemaNo = '%@';",
BOOL isUpdate = [_dataBase executeUpdate:updateStr];
if (isUpdate) {
NSLog(@"數(shù)據(jù)更新成功");
}
主要就是數(shù)據(jù)庫的SQL語句的編寫。(未完待續(xù))