取回我節省了一些圖片到SQLite數據庫在我的應用程序。我用圖形SQLite客戶端訪問數據庫,它正確保存了我的圖像的二進制文件。問題在於從數據庫中檢索它。 這是什麼樣子:保存的UIImage作爲二進制SQLite和與imageWithData
保存
-(void)insertObject:(Object *)object
{
const char* sqliteQuery = "INSERT INTO OBJECTS (OBJECTID, OBJECTIMAGE, OBJECTNAME) VALUES (?,?,?)";
sqlite3_stmt* statement;
if(sqlite3_prepare_v2(databaseHandle, sqliteQuery, -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [object.id UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2, [object.imgData bytes], [object.imgData length], SQLITE_STATIC);
sqlite3_bind_text(statement, 3, [object.name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
}
else NSLog(@"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(databaseHandle));
sqlite3_finalize(statement);
}
檢索
-(Object *)retrieveObjectWithID:(NSString*)id
{
NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT OBJECTIMAGE,OBJECTNAME FROM OBJECTS WHERE OBJECTID = '%@'", id];
NSLog(@"sqlitequery: %@",sqliteQuery);
sqlite3_stmt* statement;
Object *object = [[Object alloc]init];
object.id = id;
if(sqlite3_prepare_v2(databaseHandle, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_ROW)
{
NSUInteger length = sqlite3_column_bytes(statement, 1);
object.imgData = [NSData dataWithBytes:sqlite3_column_blob(statement, 1) length:length];
object.name = [NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 2)];
}
}
// Finalize and close database.
sqlite3_finalize(statement);
return object;
}
這是對象
@interface Object : NSObject
@property(nonatomic,strong) NSString *id;
@property(nonatomic,strong) NSString *name;
@property(nonatomic,strong) NSData *imgData;
@end
是什麼store.storeImgData? –
什麼是你的數據檢索遇到的問題? –
它檢索我一個微小的十六進制,這並不讓我建立從 –