在iphone中选择语句崩溃问题

时间:2012-08-06 06:28:17

标签: iphone

-(void)insertDataToFavourites:(int)pageid :(NSString *)description
{
    sqlite3_stmt *insertStatement = nil;
    NSString *sql;
    int returnvalue;

    sql = [NSString stringWithFormat:@"insert into AddFavorite (Id,Description) VALUES (?,?)"];

    returnvalue = sqlite3_prepare_v2(database, [sql UTF8String], -1, &insertStatement, NULL);

    if (returnvalue == 1){
        NSAssert1 (0,@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
    }
    sqlite3_bind_text(insertStatement, 2,[[tempDict objectForKey:@"pageid"] UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insertStatement, 3,[[tempDict objectForKey:@"desc"] UTF8String], -1, SQLITE_TRANSIENT);
    if (SQLITE_DONE != sqlite3_step(insertStatement)){
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    }
    else{
        sqlite3_reset(insertStatement);
    }

    sqlite3_finalize(insertStatement);

    if(sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL) != SQLITE_DONE ){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Data saved Successfully." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Data insertion error." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

}

以上代码用于将数据插入Sqlite数据库表。这显示警报数据已成功保存。  在将数据插入收藏夹列表时。

-(void)getFavoriteList
{

    if([ArrFav count] > 0)
        [ArrFav removeAllObjects];

    ArrFav = [[NSMutableArray alloc] init];

    sqlite3_stmt *selectStatement=nil;  
    NSString *sql;
    int returnvalue;

    sql=[NSString stringWithFormat:@"SELECT Description FROM AddFavorite"];

    returnvalue = sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectStatement, NULL);

    if(returnvalue==1)
    {
        NSAssert1(0, @"Error: failed to select the database with message '%s'.", sqlite3_errmsg(database));
    }

    NSMutableDictionary *dict;
    NSString *str;
    if(returnvalue == SQLITE_OK)
    {                       
        while(sqlite3_step(selectStatement) == SQLITE_ROW)
        {   
            dict = [[NSMutableDictionary alloc] init];
            [dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement,1)] forKey:@"Description"];

            [ArrFav addObject:[dict objectForKey:@"Description"]];
            NSLog(@"Favorite data is:--> %@",ArrFav);
        }       
    }
}

这是从sqlite数据库表中选择数据的代码。在 [dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement,1)] forKey:@“Description”]; 应用程序崩溃。

有人可以告诉我这个问题,为什么会出现这个问题&这是什么解决方案?

3 个答案:

答案 0 :(得分:1)

我认为您正在尝试为字典中的键设置nil值。这意味着您没有从SELECT语句中获得任何结果。请使用Mozilla sqlite编辑器检查数据库的内容,看看您的数据库中是否存在您要检索的值。

答案 1 :(得分:0)

我认为这应该是这样的..

sqlite3_bind_text(insertStatement, 1,[[tempDict objectForKey:@"pageid"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2,[[tempDict objectForKey:@"desc"] UTF8String], -1, SQLITE_TRANSIENT);

在从DB中选择数据时,你必须使用它,

[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement,0)] forKey:@"Description"]

答案 2 :(得分:0)

有几点需要注意:

1)

确保您没有使用只读版本的数据库。这将是一个刚刚添加到项目中的数据库文件,而不是应用程序库中的文档文件夹( / library / Application Support / iphone Simulator / version / applications / app id / Documents /)将文件添加到项目文件夹中将无法编写。

2)

sql = [NSString stringWithFormat:@"insert into AddFavorite (Id,Description) VALUES (?,?)"];

可能会更好(请注意“围绕值”)

sql = [NSString stringWithFormat:@"insert into AddFavorite (Id,Description) VALUES (\'%@\',\'%@\')",[tempDict objectForKey:@"pageid"], [tempDict objectForKey:@"desc"]];

3)

sqlite3_bind_text(insertStatement, 2,[[tempDict objectForKey:@"pageid"] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(insertStatement, 3,[[tempDict objectForKey:@"desc"] UTF8String], -1, SQLITE_TRANSIENT);

应该如Mehul所说,插入1和2而不是2和3,所以:

sqlite3_bind_text(insertStatement, 1,[[tempDict objectForKey:@"pageid"] UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(insertStatement, 2,[[tempDict objectForKey:@"desc"] UTF8String], -1, SQLITE_TRANSIENT);

4)

这个错误的好处在于它突出了一个重点,你必须检查从数据库返回的空字符串并正确处理错误。