开发者

Loading data from database into detailed view

I'm reading data from a sqlite database however in my detailed view one of the data items is not showing correctly. It show what looks like random memory i.e. UIDeviceRGB...(I have two strings, one that works one that doesn't). I can't work out why but when I run the debug it returns null.

Here is my code in HydratedDetailData:

if (SQLITE_DONE != sqlite3_step(detailStmt)) {
    double add = sqlite3_column_double(detailStmt, 2);
    NSString *addre开发者_JAVA百科ss = [NSString stringWithFormat:@"%@", add];
    self.ClubAddress = address;
    [address release];
}

Any help work be greatly appreciated!

code from Clubs.m

#import "Clubs.h"

static sqlite3 *database = nil;
static sqlite3_stmt *detailStmt = nil;


@implementation Clubs

@synthesize clubID, clubName, ClubAddress, isDirty, isDetailViewHydrated;

+(void) getInitialDataToDisplay:(NSString *)dbPath {
    SQLAppDelegate *appDelegate = (SQLAppDelegate *) [[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sql = "select clubID, clubName from clubNames";
        sqlite3_stmt *selectstmt;
        if (sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            while (sqlite3_step(selectstmt)== SQLITE_ROW) {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                Clubs *clubObj = [[Clubs alloc] initWithPrimaryKey:primaryKey];
                clubObj.clubName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];


                clubObj.isDirty = NO;

                [appDelegate.clubArray addObject:clubObj];
                [clubObj release];
            }
        }
    }
                   else
                   sqlite3_close(database);
                   }

                   +(void) finalizeStatements {
                       if (database) sqlite3_close(database);

                     } 


                   -(id) initWithPrimaryKey:(NSInteger) pk {
                       [super init];
                       clubID = pk;

                       isDetailViewHydrated = NO;
                       return self;
                   }



-(void) hydrateDetailViewData {
    if (isDetailViewHydrated) return; 

    if (detailStmt == nil) {
        const char *sql = "Select ClubAddress from clubNames Where clubID = ?";
         if (sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) !=SQLITE_OK)
             NSAssert1(0, @"Error while creating detail view statment. '%s'", sqlite3_errmsg(database));
         }
    sqlite3_bind_int(detailStmt, 1, clubID);

    if (SQLITE_DONE != sqlite3_step(detailStmt)) {
        double add = sqlite3_column_double(detailStmt, 0);
        NSString *address = [NSString stringWithFormat:@"%d", add];
        self.ClubAddress = address;
    }
    else
        NSAssert1(0, @"Error while getting the address of club. '%s'", sqlite3_errmsg(database));
    sqlite3_reset(detailStmt);

    isDetailViewHydrated = YES;
    }


-(void) dealloc {
    [ClubAddress release];
    [clubName release];
    [super dealloc];
}


@end


i had such a problem for reading double values, some of values read right but some read as 0 ! i have tried many things but at last my problem was solved by defining variables globally. i have defined the locally in .m file but the solution was to define them in .h file.i hope it can solve yours too. good luck


Perhaps if you used the %d formatter in your [NSString stringWithFormat:@"%@", add]; call.

(%@ is generally for strings, %d is for doubles)

After our little discussion-athon, this should fix what ails you.

if (SQLITE_DONE != sqlite3_step(detailStmt)) {
     char *db_text = sqlite3_column_text(detailStmt, 2);
      NSString *address = [NSString stringWithUTF8String: db_text];
      self.ClubAddress = address;
}

This will pull the text value from the database and convert it into an NSString*. (hopefully)....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜