欢迎投稿

今日深度:

sqlite的update遇到的问题,sqliteupdate遇到

sqlite的update遇到的问题,sqliteupdate遇到


最近在处理update的时候,发现如果不根据id来update会同时把其他的数据一起更改的现象,那么如何获取这个唯一的id呢:
- (void)loadData
{
    
    ///路径/
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                              NSUserDomainMask, YES)[0];
    
    // 拼接文件名
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"makeup.sqlite"];
    
    // 创建一个数据库的实例,仅仅在创建一个实例,并会打开数据库
    FMDatabase *db = [FMDatabase databaseWithPath:filePath];
    _db = db;
    
    
    
    // 打开数据库
    BOOL flag = [db open];
    
    
    if (flag) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
    
    
    
    // 创建数据库表
    BOOL flag1 = [_db executeUpdate:@"create table if not exists t_makeup (id integer primary key autoincrement,productName text, productBrand text, openDate text, productExpiration text, productPrice text, productTip text ,productImage blob, productKind text,openEdate text,bestDate text, isOpen text);"];
    if (flag1) {
        NSLog(@"创建成功");
    }else{
        NSLog(@"创建失败");
        
    }
    

    //这句话很关键=  =
    _makeup = [NSMutableArray array];
    
    FMResultSet *result =  [_db executeQuery:@"select * from t_makeup where productKind = ?",self.name];
    
    
    // 从结果集里面往下找
    while ([result next]) {
        
        makeupModel *makeup = [[makeupModel alloc] init];
        
        
        makeup.productName = [result stringForColumn:@"productName"];
        makeup.productBrand = [result stringForColumn:@"productBrand"];
        makeup.productPrice = [result stringForColumn:@"productPrice"];
        makeup.openDate = [result stringForColumn:@"openDate"];
        makeup.productExpirationDate = [result stringForColumn:@"productExpiration"];
        makeup.productTip = [result stringForColumn:@"productTip"];
        makeup.productImage = [result dataForColumn:@"productImage"];
        makeup.productKind = [result stringForColumn:@"productKind"];
        makeup.openExpirationDate = [result stringForColumn:@"openEdate"];
        makeup.bestDate = [result stringForColumn:@"bestDate"];
        makeup.isOpen = [result stringForColumn:@"isOpen"];
<strong><span >        makeup.ID = [result intForColumn:@"id"];
</span></strong>        
        
        
        
        [self.makeup addObject:makeup];

    }
    
 

    //[self.tableView reloadData];

}

下面是处理update;
  BOOL flag = [_db executeUpdate:@"update t_makeup set productName = ? ,productBrand = ?, openDate = ? ,productExpiration = ? ,productPrice = ?, productTip = ?, productImage = ? ,productKind = ? ,openEdate = ?, bestDate = ? ,isOpen = ? where id = ?",Model.productName,Model.productBrand,Model.openDate,Model.productExpirationDate,Model.productPrice,Model.productTip,Model.productImage,Model.productKind,Model.openExpirationDate,Model.bestDate ,Model.isOpen,<span >[NSNumber numberWithInt:Model.ID]</span>];

注意加红部分

www.htsjk.Com true http://www.htsjk.com/SQLite/29992.html NewsArticle sqlite的update遇到的问题,sqliteupdate遇到 最近在处理update的时候,发现如果不根据id来update会同时把其他的数据一起更改的现象,那么如何获取这个唯一的id呢: - (void)loadData{ ///路径/ N...
相关文章
    暂无相关文章
评论暂时关闭