【问题标题】:Select statement crashing issue in iphoneiphone中的选择语句崩溃问题
【发布时间】:2012-08-06 06:28:17
【问题描述】:
-(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"]; 应用程序正在崩溃。

谁能建议我解决这个问题,为什么会出现这个问题以及解决方案是什么?

【问题讨论】:

  • 首先你插入的数据被正确地插入到db中检查它到.db oq sqlite文件中。
  • 请检查您的选择声明。在你的日志中打印 select 语句并尝试在 sqlite 编辑器上执行相同的查询。
  • 我无法访问数据库文件保留在应用程序构建文件夹中的路径。

标签: iphone


【解决方案1】:

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

【讨论】:

  • 但是从应用程序添加数据后,应用程序数据没有显示在select语句中
  • 这意味着您没有将数据正确插入数据库。您应该返回并以适当的方式添加数据
  • 从应用程序中它没有添加...?
  • 如果我没记错的话,我们需要查看构建文件夹的数据库文件来检查从应用程序而不是 firefox 完成的更新。
【解决方案2】:

我觉得应该是这样的..

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

在从数据库中选择数据时,你应该使用这个,

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

【讨论】:

  • @Mehul...我认为它正在崩溃,因为它在 while 语句中运行了两次。
  • @JoePatel 这只是因为您从 DB 获取空字符串。请在您的数据库中查看它在从数据库中获取时不能为空
  • 主要问题是我无法从构建文件夹访问该数据库文件,因为stackoverflow.com/questions/11806278/…
【解决方案3】:

需要注意的几点:

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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多