1

Im using an existing proyect downloaded from here http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app

I like to add a new column called "image1" as BLOB in the database banklist.sqlite3 because i need to show images in FailedBanksDetailViewController.xib.

I did this:

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;
}

I have no problem with FailedBanksDetailViewController.xib, i put a UIImageView element from the library and i link it to image1View declared in FailedBanksDetailViewController.h.

But i dont know what to do setting FailedBankDatabase.m and FailedBankDetailViewController.m.

I need to show the image into the UIImageView from the database in FailedBankDetailViewController.

1 Answer 1

2

I think you want a NSData object for your image (e.g. via UIImagePNGRepresentation) and then use sqlite3_column_blob instead of sqlite3_column_text.

Sign up to request clarification or add additional context in comments.

9 Comments

please don't save binary data to your database. Rather write the image to your documents directory and store the path to that image in your database.
@bobbypage Personally, I save my images in my documents directory for performance reasons, but was wondering if you had other reasons for suggesting this...
performance really, it doesn't sound right to solve gigantic images in a database. there is more discussion here: stackoverflow.com/questions/643682/…
Thank you. could you show me how the code have to be under NSDate *updateDate = [formatter dateFromString:updatedDateString]; in FailedBankDatabase.m and under [_updatedLabel setText:[formatter stringFromDate:details.updatedDate]]; ? Im new coding and this would be very useful for me.
Here's a SO post that discusses how to do it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.