-(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?
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.