在获取sqlite数据库时遇到问题

时间:2011-06-28 06:05:47

标签: iphone objective-c sqlite

我在从 sqlite 数据库中获取数据时遇到了问题。

我尝试这样但我的数据没有被提取:

+(void) getInitialDataToDisplay:(NSString *)dbPath
{
JourneyAppDelegate *appDelegate = (JourneyAppDelegate*)[[UIApplication sharedApplication]delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
    const char *sql = "select JourneyID,JourneyName,LocationName,Description from UserJourney";
    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);
            NewJourney *journeyobj = [[NewJourney alloc]init]; 
/**/when my pointer come at this line it goes out of the scope My  journeyobj.journeyname ,journeyobj.journeylocation and journeyobj.journeydescription is not getting the value.**
            journeyobj.journeyname = [NSString stringWithUTF8String:(char*)sqlite3_column_text(selectStmt, 3)];
            journeyobj.journeylocation = [NSString stringWithUTF8String:(char*)sqlite3_column_text(selectStmt, 4)];
            journeyobj.journeydescription = [NSString stringWithUTF8String:(char*)sqlite3_column_text(selectStmt, 5)];
            journeyobj.isDirty = NO;

            [appDelegate.journeyList addObject:journeyobj];
            [journeyobj release];

        }
    }
}
else 
{
    sqlite3_close(database);
}

}

问题是什么?

提前致谢

2 个答案:

答案 0 :(得分:0)

请检查一下,您的数据库中包含这些所有字段的值。

我认为,JourneyName,LocationName,Description会有零值。

因此,在绑定这些值之前,首先检查如下:

NSString * strJourneyName = @“”;

char * JourneyNameChars =(char *)sqlite3_column_text(statement,3);

if (JourneyNameChars == NULL){

strJourneyName = @"";
}

else{

strJourneyName = [NSString stringWithUTF8String: JourneyNameChars];
}

对所有具有数据类型= TEXT的字段进行检查。

希望它对您有所帮助并解决您的问题。

干杯。

答案 1 :(得分:0)

此外,请确保您在任何线程中始终只有一个连接到sqlite的连接。多个连接不起作用。