IOS插入到db无法正常工作

时间:2013-12-05 01:10:07

标签: ios iphone sqlite sql-insert

请在下面找到我一直在尝试的内容。

-(BOOL)addItemsToTable:(NSString *)useTable WithColumnValues:(NSDictionary *) ValueObject{

    BOOL hasbeenAdded=NO;
    NSString *myColumns = @"";
    NSString *myValues = @"";
    for(int r=0; r<[self.columns count];r++){
        NSString *this_keyname = [self.columns objectAtIndex:r];
        myColumns = [myColumns stringByAppendingString:this_keyname];
        NSString *thisValue = [NSString stringWithFormat:@"'%@'", @"a"];
        myValues = [myValues stringByAppendingString:thisValue];
        if(r<(([[ValueObject allKeys]count])-1)){
            myColumns = [myColumns stringByAppendingString:@","];
            myValues = [myValues stringByAppendingString:@","];
        }
    }
    NSLog(@"%@-%@", myColumns, myValues);
    NSString *myInsert = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)",useTable, myColumns, myValues];
    char *err;
    if((sqlite3_exec(database, [myInsert UTF8String], NULL, NULL, &err))!=SQLITE_OK)
    {
        sqlite3_close(database);
        NSLog(@"Not added");
    }
    else{
        NSLog(@"Added");
        hasbeenAdded = YES;
    }
    return hasbeenAdded;

}

Mycolumns和myvalues作为数据库中的字段正确打印,但它始终进入if条件并且未添加打印。请帮助我出错的地方!!

1 个答案:

答案 0 :(得分:0)

如上所述,您可以使用错误

检查发生的确切错误
    if ( sqlite3_exec(DB, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK0
{

 }
else
{
    NSLog(@"Error : %s", error);
 }

如何使用单引号尝试插入语句,而像\'%@ \'。

 NSString *myInsert = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (\'%@\')",useTable, myColumns, myValues];