gpt4 book ai didi

iphone - iPhone 上 INSERT 的 SQLite 行为

转载 作者:IT王子 更新时间:2023-10-29 06:27:42 25 4
gpt4 key购买 nike

作为我的 iPhone 应用程序的一部分,我目前正在执行以下操作

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"];

sqlite3 *database;

if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into table (name, description, image) VALUES (?, ?, ?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text( compiledStatement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text( compiledStatement, 2, [description UTF8String], -1, SQLITE_TRANSIENT);
NSData *dataForImage = UIImagePNGRepresentation(image);
sqlite3_bind_blob( compiledStatement, 3, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);

}
if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
NSLog( @"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database));
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);

让我感到困惑的是,如果我取出该部分,

   if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
NSLog( @"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database));
}

INSERT 未保存到数据库并丢失。我假设我在这里遗漏了一些明显的东西?

最佳答案

当然不会被插入。您需要调用 sqlite3_step 才能真正执行您的语句。

检查 the documentation出。

这是 SQLITE 的东西,不是 iPhone 特定的东西。

来自文档:

After a prepared statement has been prepared using either sqlite3_prepare_v2() or sqlite3_prepare16_v2() or one of the legacy interfaces sqlite3_prepare() or sqlite3_prepare16(), this function must be called one or more times to evaluate the statement.

关于iphone - iPhone 上 INSERT 的 SQLite 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422484/

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