Sqlite3奇怪的步骤21个iOs

时间:2013-12-03 05:02:02

标签: ios sqlite

我对sqlite3有一个奇怪的问题。我的应用程序有一个数据库,我只有一个表有问题,只有一个方法与此表连接。 该表是:

#define CONTACTS_TABLE "CREATE TABLE IF NOT EXISTS `contacts` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `status` VARCHAR , `name` VARCHAR , `group_id` BIGINT , `avatator` INTEGER , `remoteId` BIGINT , `blacklist` INTEGER , `ignore` INTEGER)"

该表已有很多数据,但是当我尝试更新它时,它会显示sqlite第21步,但数据更新(!!!)。

- (BOOL)updateContactsWithContact:(ContactData *)myItem
{
    const char *dbPath = [dataBasePath UTF8String];
    if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
       // NSString *insertSqlStatement = [NSString stringWithFormat: @"UPDATE contacts SET status = ?, name = ?, avatator = ?, blacklist = ?, ignore = ? WHERE remoteId = ?"];
        NSString *insertSqlStatement = [NSString stringWithFormat: @"UPDATE contacts SET status = ?, name = ? WHERE remoteId = ?"];

        NSLog(@"item for db with name: %@ with status: %@ with isAvatator: %d with group_id : %d with isBlacklist: %d with is ignore: %d with remoteId: %d", myItem.contactName, myItem.status, myItem.isAvatator, myItem.groupId, myItem.isBlackList, myItem.isIgnore, myItem.remoteId);

        const char *insertStmt = [insertSqlStatement UTF8String];
        if  (sqlite3_prepare_v2(database, insertStmt, -1, &sqlStatement, NULL) == SQLITE_OK) {

            sqlite3_bind_text(sqlStatement, 1, [myItem.status UTF8String], -1, nil);
            sqlite3_bind_text(sqlStatement, 2, [myItem.contactName UTF8String], -1, nil);
            //sqlite3_bind_int(sqlStatement, 3, [self convertBOOLToInt:myItem.isAvatator]);
            //sqlite3_bind_int(sqlStatement, 4, [self convertBOOLToInt:myItem.isBlackList]);
            //sqlite3_bind_int(sqlStatement, 5, [self convertBOOLToInt:myItem.isIgnore]);
            sqlite3_bind_int(sqlStatement, 3, myItem.remoteId);

            NSLog(@"Step updateContactsWithRemoteId: %d\n", sqlite3_step(sqlStatement));
            if (sqlite3_step(sqlStatement) == SQLITE_DONE)
            {
                NSLog(@"updated contact from updateContactWithContactData");
                sqlite3_finalize(sqlStatement);
                sqlite3_close(database);
                return YES;
            } else {
                NSLog(@"Not updated contact from updateContactWithContactData");
                NSLog(@"Step: %d\n", sqlite3_step(sqlStatement));
                sqlite3_finalize(sqlStatement);
                sqlite3_close(database);
                return NO;
            }
            sqlite3_finalize(sqlStatement);
        } else {
            NSLog(@"updateContactWithContactData is wrong statement");
        }
    }
    sqlite3_close(database);
    return NO;
}

例如,这样的方法正确更新了这个表:

- (BOOL)updateContactsWithRemoteId:(NSInteger)remoteId withAvatatorValue:(BOOL)isAvatator
{
    const char *dbPath = [dataBasePath UTF8String];
    if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
        NSString *insertSqlStatement = [NSString stringWithFormat: @"UPDATE contacts SET avatator = ? WHERE remoteId = ?"];

        const char *insertStmt = [insertSqlStatement UTF8String];
        if  (sqlite3_prepare_v2(database, insertStmt, -1, &sqlStatement, NULL) == SQLITE_OK) {

            sqlite3_bind_int(sqlStatement, 1, [self convertBOOLToInt:isAvatator]);
            sqlite3_bind_int(sqlStatement, 2, remoteId);
            NSLog(@"Step updateContactsWithRemoteId: %d\n", sqlite3_step(sqlStatement));
            if (sqlite3_step(sqlStatement) == SQLITE_DONE)
            {
                NSLog(@"updated contact from updateContactWithContactData");
                sqlite3_finalize(sqlStatement);
                sqlite3_close(database);
                return YES;
            } else {
                NSLog(@"Not updated contact from updateContactWithContactData");
                NSLog(@"Step: %d\n", sqlite3_step(sqlStatement));
                sqlite3_finalize(sqlStatement);
                sqlite3_close(database);
                return NO;
            }
            sqlite3_finalize(sqlStatement);
        } else {
            NSLog(@"updateContactWithContactData is wrong statement");
        }
    }
    sqlite3_close(database);
    return NO;
}

我在第一种方法中找不到不正确的内容。我在这个方法中尝试了update语句的所有组合,但结果总是一样的。请帮助我,人。提前谢谢。

1 个答案:

答案 0 :(得分:2)

每次“更新”或“插入”查询只能拨打sqlite3_step一次。如上所述,你至少称它为两次,也许是3次。为什么?您还需要进行更好的错误检查。试试这个:

- (BOOL)updateContactsWithContact:(ContactData *)myItem {
    BOOL ok = YES;
    const char *dbPath = [dataBasePath UTF8String];
    if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
       // NSString *insertSqlStatement = [NSString stringWithFormat: @"UPDATE contacts SET status = ?, name = ?, avatator = ?, blacklist = ?, ignore = ? WHERE remoteId = ?"];
        NSString *insertSqlStatement = [NSString stringWithFormat: @"UPDATE contacts SET status = ?, name = ? WHERE remoteId = ?"];

        NSLog(@"item for db with name: %@ with status: %@ with isAvatator: %d with group_id : %d with isBlacklist: %d with is ignore: %d with remoteId: %d", myItem.contactName, myItem.status, myItem.isAvatator, myItem.groupId, myItem.isBlackList, myItem.isIgnore, myItem.remoteId);

        const char *insertStmt = [insertSqlStatement UTF8String];
        int res;
        if  ((res = sqlite3_prepare_v2(database, insertStmt, -1, &sqlStatement, NULL)) == SQLITE_OK) {

            sqlite3_bind_text(sqlStatement, 1, [myItem.status UTF8String], -1, nil);
            sqlite3_bind_text(sqlStatement, 2, [myItem.contactName UTF8String], -1, nil);
            //sqlite3_bind_int(sqlStatement, 3, [self convertBOOLToInt:myItem.isAvatator]);
            //sqlite3_bind_int(sqlStatement, 4, [self convertBOOLToInt:myItem.isBlackList]);
            //sqlite3_bind_int(sqlStatement, 5, [self convertBOOLToInt:myItem.isIgnore]);
            sqlite3_bind_int(sqlStatement, 3, myItem.remoteId);

            if ((res = sqlite3_step(sqlStatement)) == SQLITE_DONE) {
                NSLog(@"updated contact from updateContactWithContactData");
            } else {
                NSLog(@"Not updated contact from updateContactWithContactData");
                NSLog(@"Step: %d\n", res, sqlite3_errmsg(database));
                ok = NO;
            }
            sqlite3_finalize(sqlStatement);
        } else {
            NSLog(@"updateContactWithContactData is wrong statement: %d - %s", res, sqlite3_errmsg(database));
            ok = NO;
        }

        sqlite3_close(database);
    } else {
        NSLog(@"Unable to open database: %s", sqlite3_errmsg(database));
        ok = NO;
    }

    return ok;
}