iphone - iPhone 上 INSERT 的 SQLite 行为

标签 iphone objective-c sqlite

作为我的 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/

相关文章:

iphone - iPhone 5 + iOS6 如何决定应用程序是否必须在信箱模式下运行

ios - 如何在运行时在 Objective-C 中查找字符串常量?

ios - 在 iOS 中从数组中获取重复项和原始项

iphone - DB Wrapper 不响应消息?

iphone - 如何在不共享 iPhone/iPad 源代码的情况下将我们的应用程序代码集成到其他应用程序中?

android - iOS 是否具有与 Android 一样的应用内更新功能?

iphone - 是否可以在不越狱的情况下在 iOS/iPhone 上获取来电显示?

iphone - 在 Objective C 中存储资源文件名的正确方法

python - 在 Colab 中升级 SQLite

django - 夹层富文本字段