2
votes
-(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];
    }

}

Above code i am doing for the insertion of the data to Sqlite Database table. This is showing me alert Data saved Successfully. at the time of inserting data to Favorite list.

-(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);
        }       
    }
}

This is the code to select data from sqlite database table. at the line [dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement,1)] forKey:@"Description"]; application is getting crashed.

Can anybody suggest me for the problem, why this problem is occured & what is the solution of this?

3
first of all you insert data is properly inserted into db check it out into .db oq sqlite file.Nimit Parekh
Please check your select statement. Print the select statement in ur log and try to execute the same query on sqlite editor.A for Alpha
I am not able to reach at the path where the database file remains in the build folder of the application.JoePatel

3 Answers

1
votes

I think you are trying to set a nil value for a key in dictionary. This means you are not getting any result from your SELECT statement. Please check the contents of your DB by using Mozilla sqlite editor and see if the value your are trying to retrieve exists in your DB.

0
votes

I think this should be like this..

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

and while selecting data from DB, You should have to use this,

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

A few things to note:

1)

Make sure you are not using a readonly version of the DB. This would be a database file that is just added to your project and not to the documents folder in your applications library (/library/Application Support/iphone Simulator/version/applications/app id/Documents/) Adding the file to your project folder won't allow you to write.

2)

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

might be better as (note the ' ' around the 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);

Should be as Mehul said, insert into 1 and 2 not 2 and 3, so:

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)

The positive thing with this bug is that it highlights an important point, that you must check for null strings to be returned from your database and handle the error properly.