2015-05-09 49 views
1

数据添加到一个数据库中,我想,当我们把这个InsUpdateDelData其返回假执行插入查询查询工作正常则表示数据没有插入如何使用SQLite经理

- (IBAction)addToCart:(id)sender { 

    AppDelegate *obj = (AppDelegate *)[[UIApplication sharedApplication]delegate] ; 
    NSString *insert = @"[email protected]" ; 


    NSString *insertSQL = [NSString stringWithFormat: 
    @"INSERT INTO cart_user(user_id,product_price,product_type,categories_type,product_images,description) values('%@','%@','%@','%@','%@','%@')",insert,handBegsImages.product_price,handBegsImages.product_tagline,handbegCategoriess.handbegid,handBegsImages.main_image,handBegsImages.product_description]; 
    BOOL abc = [obj InsUpdateDelData:insertSQL]; 
    NSLog(@"print the value of abc %@=", abc) ; 
    if (abc == TRUE) { 
     NSLog(@"@ Data was Inserted"); 
    } 
    else{ 
      [Utility showAlertView:@"Plz try again message" message:@"Again" viewcontroller:self]; 
    } 
    } 


-(BOOL)InsUpdateDelData:(NSString*)SqlStr 
{ 
    if([SqlStr isEqual:@""]) 
     return NO; 

    BOOL RetrunValue; 
    RetrunValue = NO; 
    const char *sql = [SqlStr cStringUsingEncoding:NSUTF8StringEncoding]; 
    sqlite3_stmt *stmt; 
    if (sqlite3_prepare_v2(database, sql, -1, &stmt, nil) == SQLITE_OK) 
     RetrunValue = YES; 

    if(RetrunValue == YES) 
    { 
     if(sqlite3_step(stmt) != SQLITE_DONE) { 

     } 
     sqlite3_finalize(stmt); 
    } 
    return RetrunValue; 
} 
+0

报告不是错误 –

+0

的NSLog(@“打印sqlite3的价值%s =“,sqlite3_errmsg(database)); 我的问题是解决这个问题的解决方案 –

+0

非常好。顺便说一句,你可能想要小心构建这样的SQL语句。如果其中任何一个字符串值碰巧有一个撇号,你的SQL将失败。最好使用'?'占位符,然后将值绑定到它们。如果你正确地做到这一点,你的代码会很快变得麻烦,所以你可能会考虑像[FMDB](https://github.com/ccgus/fmdb)这样的函数,它具有像'executeUpdate'和'executeQuery'这样的函数,值,但为您执行必要的'sqlite3_bind_text'调用。 – Rob

回答

0

如果InsUpdateDelData回到NO那么这意味着sqlite3_prepare_v2没有返回SQLITE_OK。如果你想知道为什么它没有返回SQLITE_OK,则记录错误后立即sqlite3_prepare_v2失败,但调用任何其它的SQLite函数之前:

NSLog(@"err=%s", sqlite3_errmsg(database));