0

当我将数组元素插入 sqlite 时,它​​会正确插入。它只需要数组中的第一个元素,其余元素在插入时会显示错误。我的代码是:

NSString *path=[self getDBPath];
sqlite3_stmt *insert_statement = nil;    

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
    for (int i = 0; i < [array count]; i++) 
    {
        NSLog(@"Array Count is %d",[array count]);
        NSString *name=[[array objectAtIndex:i]objectForKey:@"name"];
        NSString *add=[[array objectAtIndex:i]objectForKey:@"address"];
       
        if (insert_statement == nil) {
            NSString *statement1 = [NSString stringWithFormat:@"insert into tableName (name,address) Values( ?,?)",name,add];
            const char *insertSql = [statement1 UTF8String];
            if(sqlite3_prepare_v2(database, insertSql, -1, &insert_statement, NULL) != SQLITE_OK){
                NSLog(@"Error while creating insert statement.");
                insert_statement = nil;
                continue;
            }
            
        sqlite3_bind_text(insert_statement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 2, [add UTF8String], -1, SQLITE_TRANSIENT);
        

        if(SQLITE_DONE != sqlite3_step(insert_statement)){
            //NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
            NSLog(@"Error while inserting data.");
            insert_statement = nil;
            continue;
        }
        else{}
        
            sqlite3_reset(insert_statement);
            sqlite3_finalize(insert_statement);
            insert_statement = nil;
        }
    }
sqlite3_close(database);
}

为什么它只允许一条记录?

4

3 回答 3

3

这是由于每次循环中的 finalize 语句而发生的。所以它不会让在第一个对象之后插入其他对象。

只需"sqlite3_finalize(insert_statement);"从循环中删除并设置nil为插入语句"insert_statement = nil;"

完成循环后。确保将"sqlite3_reset(insert_statement);"for 每个对象放在循环中。它重置语句,无需设置nil循环中的语句。

试试这段代码,它对我有用:

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {  

    for (int i = 0; i < [array count]; i++)
    {
        NSLog(@"Array Count is %d",[array count]);
        NSString *name=[[array objectAtIndex:i]objectForKey:@"name"];
        NSString *add=[[array objectAtIndex:i]objectForKey:@"address"];
        
        if (insert_statement == nil) {
            NSString *statement1 = [NSString stringWithFormat:@"insert into tableName (name,address) Values( ?,?)",name,add];
            const char *insertSql = [statement1 UTF8String];
            if(sqlite3_prepare_v2(database, insertSql, -1, &insert_statement, NULL) != SQLITE_OK){
                NSLog(@"Error while creating insert statement.");
                insert_statement = nil;
                continue;
            }
            
            sqlite3_bind_text(insert_statement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(insert_statement, 2, [add UTF8String], -1, SQLITE_TRANSIENT);
            
            
            if(SQLITE_DONE != sqlite3_step(insert_statement)){
                //NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                NSLog(@"Error while inserting data.");
                insert_statement = nil;
                continue;
            }
            else{
            
            sqlite3_reset(insert_statement);
            
            
            }
        }
    }
    insert_statement = nil;
    sqlite3_finalize(insert_statement);
    sqlite3_close(database);
}
于 2012-10-26T13:37:25.647 回答
2

试试这个代码它对我有用

      for(int i=0;i<[data count];i++) 
        {
            NSMutableDictionary *myDict = [data objectAtIndex:i];

            selectSql=[NSString stringWithFormat:@"INSERT INTO [commentTable] values('%@','%@')",[myDict valueForKey:@"usertype"],[myDict valueForKey:@"userid"]];

            if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
            {
                if (sqlite3_step(statement) == SQLITE_DONE)
                {
                    ret = YES;
                    NSLog(@"DB SUPPORT - commentTable INSERTED");
                }
                else
                {
                    NSLog(@"DB SUPPORT - ERROR commentTable INSERT");
                }

            }
            else
            {
                NSLog(@"DB SUPPORT - Sql Preparing Error ( INSERT commentTable)");
            }

            sqlite3_finalize(statement);
        }


        sqlite3_close(database);
于 2012-10-26T13:04:29.627 回答
0

您可以使用 :

for (int i = 0; i < [mutArray count]; i++)
    {
       NSString *string = [mutArray objectAtIndex:i];
       // insert query
       insert into table_name ('string') values(column_name);
    }

参考:如何将 nsmutable 数组添加到 sqlite 数据库表中

于 2012-10-26T13:11:01.297 回答