我目前正在做我的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来实际执行您的语句.
检查文档.
这是一个SQLITE的东西,而不是iPhone特有的东西.
从文档:
在使用sqlite3_prepare_v2()或sqlite3_prepare16_v2()或其中一个遗留接口sqlite3_prepare()或sqlite3_prepare16()准备好预准备语句后,必须调用此函数一次或多次以评估该语句.