将数据库中的数据加载到详细视图中

时间:2011-04-07 01:12:25

标签: iphone xcode sqlite

我正在从sqlite数据库中读取数据,但在我的详细视图中,其中一个数据项未正确显示。它显示了看起来像随机存储器的东西,即UIDeviceRGB ......(我有两个字符串,一个不能工作的字符串)。我无法理解为什么但是当我运行调试时它返回null。

这是我在HydratedDetailData中的代码:

if (SQLITE_DONE != sqlite3_step(detailStmt)) {
    double add = sqlite3_column_double(detailStmt, 2);
    NSString *address = [NSString stringWithFormat:@"%@", add];
    self.ClubAddress = address;
    [address release];
}

非常感谢任何帮助工作!

来自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

2 个答案:

答案 0 :(得分:1)

我有一个读取双值的问题,一些值读取正确但有些值读为0!我尝试了很多东西,但最后我的问题通过全局定义变量来解决。我在.m文件中定义了本地,但解决方案是在.h文件中定义它们。我希望它也能解决你的问题。 祝你好运

答案 1 :(得分:0)

也许如果您在[NSString stringWithFormat:@“%@”中使用了%d格式化程序,请添加]​​;调用

(%@通常用于字符串,%d用于双打)

经过我们的小讨论后,这应该可以解决你的问题。

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

这将从数据库中提取文本值并将其转换为NSString *。 (希望)....