문제

iPhone 개발에 SQLITE3을 사용하고 있으며 몇 가지 삽입 문을 트랜잭션으로 래핑하려고합니다. 현재 아래 코드가 제대로 작동하지만 다른 질문을 읽은 후에는 각각 하나가 아닌 한 트랜잭션에서이를 갖는 것이 더 낫다는 것을 깨달았습니다. CAPI 호출을 찾을 수 없었습니다. 코드 중 일부는 Objective-C에 있지만 실제로 질문과 관련이 있다고 생각하지 않습니다.

- (void)saveAnimals {
    //Insert all the animals into the zoo database
    int i;

    const char *sql = "insert into Animal(Zoo_ID, Animal_Num, Animal_Text) Values(?, ?, ?)";
    for (i = 0; i < ([[self animalArray] count] - 1); i++) {

        if(addStmt == nil) {
            if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        }
        int animalNum = [[[self animalArray] objectAtIndex:i] animal_Number];
        NSString *animalText = [[NSString alloc] initWithString:[[[self animalArray] objectAtIndex:i] animal_Text]];
        sqlite3_bind_int(addStmt, 1, zoo_ID);   
        sqlite3_bind_int(addStmt, 2, animalNum);    
        sqlite3_bind_text(addStmt, 3, [animalText UTF8String], -1, SQLITE_TRANSIENT);
        [animalText release];
        if(SQLITE_DONE != sqlite3_step(addStmt)) {
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
        }

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

내가해야한다고 생각하는 것은 SQLite3_prepare_v2 명령을 For Loop에서 가져오고, 거래를 시작하고, For Loop을 통과하고, 거래를 커밋하는 것입니다. 그러나 "거래 시작"및 "거래를 커밋"하는 것이 무엇인지 잘 모르겠습니다. 그리고 여전히 sqlite3_step을 사용합니까? 당신의 도움을 주셔서 감사합니다.

도움이 되었습니까?

해결책

다음과 함께 거래를 시작합니다. sqlite3_exec(db, "BEGIN", 0, 0, 0);

거래를 커밋합니다. sqlite3_exec(db, "COMMIT", 0, 0, 0);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top