我正在使用从此处下载的现有proyect
http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app
我想在数据库banklist.sqlite3中添加一个名为“image1”的新列作为BLOB,因为我需要在FailedBanksDetailViewController.xib中显示图像。
我这样做:
FailedBanksDetail.h
#import <Foundation/Foundation.h>
@interface FailedBankDetails : NSObject {
int _uniqueId;
NSString *_name;
NSString *_city;
NSString *_state;
int _zip;
NSDate *_closeDate;
NSDate *_updatedDate;
NSString *_image1;
}
@property (nonatomic, assign) int uniqueId;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *state;
@property (nonatomic, assign) int zip;
@property (nonatomic, retain) NSDate *closeDate;
@property (nonatomic, retain) NSDate *updatedDate;
@property (nonatomic, copy) NSString *image1;
- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name city:(NSString *)city
state:(NSString *)state zip:(int)zip closeDate:(NSDate *)closeDate
updatedDate:(NSDate *)updatedDate image1:(NSString *)image1;
@end
FailedBanksDetail.m
#import "FailedBankDetails.h"
@implementation FailedBankDetails
@synthesize uniqueId = _uniqueId;
@synthesize name = _name;
@synthesize city = _city;
@synthesize state = _state;
@synthesize zip = _zip;
@synthesize closeDate = _closeDate;
@synthesize updatedDate = _updatedDate;
@synthesize image1 = _image1;
- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name
city:(NSString *)city state:(NSString *)state zip:(int)zip closeDate:(NSDate *)closeDate
updatedDate:(NSDate *)updatedDate image1:(NSString *)image1 {
if ((self = [super init])) {
self.uniqueId = uniqueId;
self.name = name;
self.city = city;
self.state = state;
self.zip = zip;
self.closeDate = closeDate;
self.updatedDate = updatedDate;
self.image1 = image1;
}
return self;
}
- (void) dealloc
{
self.name = nil;
self.city = nil;
self.state = nil;
self.closeDate = nil;
self.updatedDate = nil;
self.image1 = nil;
[super dealloc];
}
@end
FailedBankDetailViewController.h
#import <UIKit/UIKit.h>
@interface FailedBanksDetailViewController : UIViewController {
UILabel *_nameLabel;
UILabel *_cityLabel;
UILabel *_stateLabel;
UILabel *_zipLabel;
UILabel *_closedLabel;
UILabel *_updatedLabel;
int _uniqueId;
UIImageView *_image1View;
}
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *cityLabel;
@property (nonatomic, retain) IBOutlet UILabel *stateLabel;
@property (nonatomic, retain) IBOutlet UILabel *zipLabel;
@property (nonatomic, retain) IBOutlet UILabel *closedLabel;
@property (nonatomic, retain) IBOutlet UILabel *updatedLabel;
@property (nonatomic, assign) int uniqueId;
@property (nonatomic, retain) IBOutlet UIImageView *image1View;
@end
FailedBankDetailViewController.m
// In the #import section
#import "FailedBankDatabase.h"
#import "FailedBankDetails.h"
// In the @implementation section
@synthesize nameLabel = _nameLabel;
@synthesize cityLabel = _cityLabel;
@synthesize stateLabel = _stateLabel;
@synthesize zipLabel = _zipLabel;
@synthesize closedLabel = _closedLabel;
@synthesize updatedLabel = _updatedLabel;
@synthesize uniqueId = _uniqueId;
@synthesize image1View = _image1View;
// In the dealloc section AND the viewDidUnload section
self.nameLabel = nil;
self.cityLabel = nil;
self.stateLabel = nil;
self.zipLabel = nil;
self.closedLabel = nil;
self.updatedLabel = nil;
self.image1View = nil;
//In the viewWillAppear method
- (void)viewWillAppear:(BOOL)animated {
FailedBankDetails *details = [[FailedBankDatabase database]
failedBankDetails:_uniqueId];
if (details != nil) {
[_nameLabel setText:details.name];
[_cityLabel setText:details.city];
[_stateLabel setText:details.state];
[_zipLabel setText:[NSString stringWithFormat:@"%d", details.name]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM dd, yyyy"];
[_closedLabel setText:[formatter stringFromDate:details.closeDate]];
[_updatedLabel setText:[formatter stringFromDate:details.updatedDate]];
[_image1View "I DONT KNOW WHAT TO DO HERE"
}
}
FailedBankDatabase.m
// In the #import section
#import "FailedBankDetails.h"
// Anywhere inside the @implementation
- (FailedBankDetails *)failedBankDetails:(int)uniqueId {
FailedBankDetails *retval = nil;
NSString *query = [NSString stringWithFormat:@"SELECT id, name, city, state,
zip, close_date, updated_date, image1 FROM failed_banks WHERE id=%d", uniqueId];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int uniqueId = sqlite3_column_int(statement, 0);
char *nameChars = (char *) sqlite3_column_text(statement, 1);
char *cityChars = (char *) sqlite3_column_text(statement, 2);
char *stateChars = (char *) sqlite3_column_text(statement, 3);
int zip = sqlite3_column_int(statement, 4);
char *closeDateChars = (char *) sqlite3_column_text(statement, 5);
char *updatedDateChars = (char *) sqlite3_column_text(statement, 6);
"I DONT KNOW WHAT TO DO HERE"
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
NSString *city = [[NSString alloc] initWithUTF8String:cityChars];
NSString *state = [[NSString alloc] initWithUTF8String:stateChars];
NSString *closeDateString =
[[NSString alloc] initWithUTF8String:closeDateChars];
NSString *updatedDateString =
[[NSString alloc] initWithUTF8String:updatedDateChars];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *closeDate = [formatter dateFromString:closeDateString];
NSDate *updateDate = [formatter dateFromString:updatedDateString];
"I DONT KNOW WHAT TO DO HERE"
retval = [[[FailedBankDetails alloc] initWithUniqueId:uniqueId name:name
city:city state:state zip:zip closeDate:closeDate
updatedDate:updateDate] autorelease];
[name release];
[city release];
[state release];
[closeDateString release];
[updatedDateString release];
[formatter release];
break;
}
sqlite3_finalize(statement);
}
return retval;
}
我对FailedBanksDetailViewController.xib没问题,我从库中放置了UIImageView元素,并将其链接到FailedBanksDetailViewController.h中声明的image1View。
但是我不知道该怎么做设置FailedBankDatabase.m和FailedBankDetailViewController.m。
我需要将图像从FailedBankDetailViewController中的数据库显示到UIImageView中。
最佳答案
我认为您想要为图像使用NSData
对象(例如,通过UIImagePNGRepresentation
),然后使用sqlite3_column_blob
而不是sqlite3_column_text
。