我有一个应用程序,它存储有关帐户的一些信息,包括图像。一切都很好:创建了表,可以保存数据,但图像没有(我不明白图像是否没有保存或无法从数据库中检索)。我的代码:
数据库表:
static const char *accountsTable = "CREATE TABLE IF NOT EXISTS tbl_accounts (unique_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, provider_id INTEGER, login TEXT, password TEXT, threshold INTEGER, is_need_push INTEGER, comment TEXT, image BLOB)";
我的插入方法:
-(BOOL) createAccountWithAccountData:(AccountsData *) accountData
{
NSInteger pushNotifications = accountData.isNeedPushNotifications ? 1 : 0;
const char *dbPath = [dataBasePath UTF8String];
if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
NSString *insertSqlStatement = [NSString stringWithFormat: @"INSERT INTO tbl_accounts (provider_id, login, password, threshold, is_need_push, comment, image) values ('%d', '%@', '%@', '%d', '%d', '%@', '?')", accountData.providerId, accountData.logIn, accountData.password, accountData.threshold, pushNotifications, accountData.comment];
const char *insertStmt = [insertSqlStatement UTF8String];
if (sqlite3_prepare_v2(database, insertStmt, -1, &sqlStatement, NULL) == SQLITE_OK ) {
if (accountData.image != nil) {
NSLog(@"Image not null");
NSData *imageData = UIImageJPEGRepresentation(accountData.image, 1.0);
sqlite3_bind_blob(sqlStatement, 7, [imageData bytes], [imageData length], nil);
} else {
NSLog(@"image is nil");
}
if (sqlite3_step(sqlStatement) == SQLITE_DONE)
{
NSLog(@"Successfully created account data");
sqlite3_reset(sqlStatement);
sqlite3_close(database);
return YES;
} else {
NSLog(@"Unable to create account data");
sqlite3_reset(sqlStatement);
sqlite3_close(database);
return NO;
}
}
}
return NO;
}
我的获取所有帐户方法:
-(NSArray *) getAllAccounts
{
const char *dbPath = [dataBasePath UTF8String];
NSMutableArray *allAccounts = [[NSMutableArray alloc] init];
if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
NSLog(@"DB Opened");
NSString *findSqlStatement = [NSString stringWithFormat: @"SELECT * FROM tbl_accounts"];
const char *findStmt = [findSqlStatement UTF8String];
if (sqlite3_prepare_v2(database, findStmt, -1, &sqlStatement, NULL) == SQLITE_OK)
{
NSLog(@"statement was prepared");
while (sqlite3_step(sqlStatement) == SQLITE_ROW)
{
NSLog(@"Into the while loop");
NSInteger uniqueId = [[NSString stringWithUTF8String:(const char *) sqlite3_column_text(sqlStatement, 0)] integerValue];
NSInteger providerId = [[NSString stringWithUTF8String:(const char *) sqlite3_column_text(sqlStatement, 1)] integerValue];
NSString *logIn = [NSString stringWithUTF8String:
(const char *) sqlite3_column_text(sqlStatement, 2)];
NSString *password = [NSString stringWithUTF8String:
(const char *) sqlite3_column_text(sqlStatement, 3)];
NSInteger threshold = [[NSString stringWithUTF8String:(const char *) sqlite3_column_text(sqlStatement, 4)] integerValue];
NSInteger pushNotif = [[NSString stringWithUTF8String:(const char *) sqlite3_column_text(sqlStatement, 5)] integerValue];
NSString *comment = [NSString stringWithUTF8String:
(const char *) sqlite3_column_text(sqlStatement, 6)];
BOOL isNeedNotif = [self convertNSInteger:pushNotif];
int length = sqlite3_column_bytes(sqlStatement, 7);
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(sqlStatement, 7) length:length];
NSLog(@"itemLogin: %@", logIn);
NSLog(@"password : %@", password);
NSLog(@"data is: %@", data);
NSLog(@"comment is: %@", comment);
NSLog(@"isNeedNotif: %hhd", isNeedNotif);
UIImage *imageFromDb = nil;
if (data != nil)
imageFromDb = [[UIImage alloc] initWithData:data];
else
NSLog(@"No image");
if (imageFromDb) {
NSLog(@"Image");
} else {
NSLog(@"NoImage");
}
AccountsData *item = [[AccountsData alloc] initWithProviderId:providerId logIn:logIn password:password threshold:threshold isNeedPushNotifications:isNeedNotif comment:comment image:imageFromDb];
item.unique_id = uniqueId;
[allAccounts addObject:item];
NSLog(@"Item added to array");
NSLog(@"Array count: %d", [allAccounts count]);
}
}
sqlite3_reset(sqlStatement);
sqlite3_close(database);
}
return allAccounts;
}
我已经在模拟器上对其进行了测试,图像数据是(NSDATA,根据 NSLog):数据是:<3f>
请帮我!!!