【发布时间】:2010-11-28 04:37:56
【问题描述】:
作为我的 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 未保存到数据库并丢失。我假设我在这里遗漏了一些明显的东西?
【问题讨论】:
-
我很好奇您不使用 Core Data 的原因。
-
我通常使用一些围绕 SQLite 的包装器,或者现在经常使用 Core Data。但是我想知道如何直接使用该库,所以...
标签: iphone objective-c sqlite