| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
#pragma mark - FMDB資料庫操作//插入-(void)insert{ if ([_db open]) { NSString *sql1 = [NSString stringWithFormat: @"INSERT INTO ‘%@‘ (‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘) VALUES (‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘, ‘%@‘)", TABLENAME, @"starting", @"destination", @"goodsName", @"car", @"goodsWeight", @"goodsVolume",@"intentionPrice",@"bail",@"mark",@"status",_startingLabel.text,_destinationLabel.text,goodsNameTextField.text,selectVehicleLabel.text,goodsWeightTextField.text,goodsVolumeTextField.text,intentionPriceTextField.text,bailTextField.text,markLabel.text,@"0"]; BOOL res = [_db executeUpdate:sql1]; if (!res) { NSLog(@"error when insert db table"); } else { NSLog(@"success to insert db table"); } [_db close]; }}//修改-(void)update{ if ([_db open]) { NSString *updateSql = [NSString stringWithFormat:@"update %@ set %@=‘%@‘ where %@=‘%@‘",TABLENAME,DESTINATION,@"目的地 上海",STARTING,@"蕪湖 出發地"]; BOOL res = [_db executeUpdate:updateSql]; if (!res) { NSLog(@"error when insert db table"); } else { NSLog(@"success to insert db table"); } [_db close]; } }//刪除-(void)delete{ if ([_db open]) { NSString *deleteSql = [NSString stringWithFormat: @"delete from %@ where %@ = ‘%@‘", TABLENAME, STARTING, @"蕪湖 出發地"]; BOOL res = [_db executeUpdate:deleteSql]; if (!res) { NSLog(@"error when insert db table"); } else { NSLog(@"success to insert db table"); } [_db close]; }} //查詢 - (void)query { if ([_db open]) { NSString * sql = [NSString stringWithFormat: @"SELECT * FROM %@",TABLENAME]; FMResultSet * rs = [_db executeQuery:sql]; while ([rs next]) { int Id = [rs intForColumn:@"ID"]; NSString * name = [rs stringForColumn:STARTING]; NSString * age = [rs stringForColumn:DESTINATION]; NSString * address = [rs stringForColumn:GOODSNAME]; NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address); } [_db close]; }} |