gpt4 book ai didi

ios - SQLite 没有在 iphone 应用程序中获取最后插入的行 ID

转载 作者:行者123 更新时间:2023-11-28 22:03:06 24 4
gpt4 key购买 nike

我想从 SQLite 表中最后插入的值中获取 ID。我使用以下方法来实现这一点。它没有得到值(value)。

NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO beacons (beacon_id, vendor_name, enter_message, enter_image, vendor_image, time_interval, received_date, uuid, major, minor) VALUES (\'%@\', \'%@\', \'%@\', \'%@\', \'%@\',%d, \'%@\', \'%@\', %d, %d)",
id, vendorName, sampleMessage, entryImage, thumbnail, [interval intValue], date, uuid, [majorId intValue], [minorId intValue]];

const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_beaconDB, insert_stmt,
-1, &statement, NULL);
long long lastRowId = sqlite3_last_insert_rowid(_beaconDB);
NSString *rowId = [NSString stringWithFormat:@"%d", (int)lastRowId];
[identity insertObject:rowId atIndex:i];
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Successfully saved");
}

在上面的代码中,值被成功地插入到数据库表中。但是最后插入的值的唯一 ID 没有返回。

提前致谢。

最佳答案

您需要在使用 sqlite3_step 插入行后获取最后一行 ID。

NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO beacons (beacon_id, vendor_name, enter_message, enter_image, vendor_image, time_interval, received_date, uuid, major, minor) VALUES (\'%@\', \'%@\', \'%@\', \'%@\', \'%@\',%d, \'%@\', \'%@\', %d, %d)",
id, vendorName, sampleMessage, entryImage, thumbnail, [interval intValue], date, uuid, [majorId intValue], [minorId intValue]];

const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_beaconDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Successfully saved");
long long lastRowId = sqlite3_last_insert_rowid(_beaconDB);
NSString *rowId = [NSString stringWithFormat:@"%d", (int)lastRowId];
[identity insertObject:rowId atIndex:i];
}

仅供引用 - 您还需要更多的错误检查,并且需要在完成后清理准备好的语句。

关于ios - SQLite 没有在 iphone 应用程序中获取最后插入的行 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24645574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com