0

我正在使用以下代码将数据插入数据库,但未将数据插入数据库中我正在使用以下代码

我正在将数据插入 iphone 应用程序,但它没有插入

我有表survey_question_master(question_id,question_text,question_type)

- (void) addCoffee {
    if(addStmt == nil) {
        const char *sql = "insert into survey_question_master(question_text,question_type) Values( ? ,?)";

        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
    }

    sqlite3_bind_text(addStmt, 1, [question_text UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 2, [question_type UTF8String], -1, SQLITE_TRANSIENT);

    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        question_ID = sqlite3_last_insert_rowid(database);

    //Reset the add statement.
    sqlite3_reset(addStmt);
}



   - (void) addCoffee:(DataController *)coffeeObj {

    //Add it to the database.
    [coffeeObj addCoffee];
   //Add it to the coffee array.
    [coffeeArray addObject:coffeeObj];
     NSLog(@"Succeessfully Added");
    }
4

1 回答 1

0
- (NSString *) getDBPath 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
}

-(void)SaveData:(NSMutableArray *)insertDataArray
{

    sqlite3_stmt *stmt;
    const char *dbPath = [[self getDBPath] UTF8String];
    if(sqlite3_open(dbPath, &database)==SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:@"insert into items (c1,c2,c3,c4 , c5 ,c6,c7,c8,c9) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",[insertDataArray objectAtIndex:0],[insertDataArray objectAtIndex:1],[insertDataArray objectAtIndex:2],[insertDataArray objectAtIndex:3],[insertDataArray objectAtIndex:4],[insertDataArray objectAtIndex:5],[insertDataArray objectAtIndex:6],[insertDataArray objectAtIndex:7],[insertDataArray objectAtIndex:8]];

        NSLog(@"%@",insertSQL);
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt, -1, &stmt, NULL);

        if(sqlite3_step(stmt)==SQLITE_DONE)
        {
            NSLog(@"insert success");
        }
        else
        {
            NSLog(@"insert un success");
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }
        int success=sqlite3_step(stmt);

        if (success == SQLITE_ERROR) 
        {
            NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
        }        sqlite3_finalize(stmt);
        sqlite3_close(database);

    }

}
于 2012-07-30T12:57:12.917 回答