简体   繁体   中英

database issues for insert special character in iphone?

i am new to sqlite database & iphone developement

am insert data into database as

am insert description string is like dz 
• 500 grams Chicken breasts
• 6-8 nos. Green chillies
• 2 tblsp Coriander leaves
• 2 tblsp Mint leaves
• 12 nos. Garlic
• 8-10 nos. Almonds
• 3/4 cup Fresh cream
• 1 inch Ginger
• 1 tblsp Lemon juice
• Oil
• Salt
this is code for inserting 

-(void)insertDataToFavourites:(int)pageid111 :(NSString *)description
{
    NSLog(@"%@",description);
    const char *sqlStatement = "INSERT INTO AddFavoritenew (Id,Description) VALUES (?,?)";
    sqlite3_stmt *compiled_statement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiled_statement, NULL) == SQLITE_OK)
    {
        sqlite3_bind_int(compiled_statement, 1, pageid111);
        sqlite3_bind_text(compiled_statement, 2, [description UTF8String] , -1, SQLITE_TRANSIENT);
        [ArrAddData addObject:[NSString stringWithFormat:@"%d", pageid111]];
        NSLog(@"ArrAddData record is:--> %@",ArrAddData);
    }

    if(sqlite3_step(compiled_statement) != SQLITE_DONE )
    {
        //        NSLog( @"Error: %s", sqlite3_errmsg(database) );
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!"
                                                        message:@"alerady you added this sms to favorite list."
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    else
    {
        //        NSLog( @"Insert into row id = %lld", sqlite3_last_insert_rowid(database));
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!"
                                                        message:@"Data saved to Favorite."
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

    sqlite3_finalize(compiled_statement);    
}

am inserting the above data into database but problem is

when am showing saved data from database is it showing like as

• 500 grams Chicken breasts
• 6-8 nos. Green chillies
• 2 tblsp Coriander leaves
• 2 tblsp Mint leaves
• 12 nos. Garlic
• 8-10 nos. Almonds
• 3/4 cup Fresh cream
• 1 inch Ginger
• 1 tblsp Lemon juice
• Oil
• Salt

geting favirote list code as

-(void)getFavoriteList
{
    if([ArrFav count] > 0)
        [ArrFav removeAllObjects];

    ArrFav = [[NSMutableArray alloc] init];

    sqlite3_stmt *selectStatement=nil;  
    const char *sql ="SELECT Description FROM AddFavoritenew";
    int returnvalue;

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

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

    if(returnvalue == SQLITE_OK)
    {                       
        while(sqlite3_step(selectStatement) == SQLITE_ROW)
        {   
            char *str = (char *)sqlite3_column_text(selectStatement,0);
            [ArrFav addObject:[NSString stringWithFormat:@"%s",str]];
            NSLog(@"Favorite data is:--> %s",str);
        }       
//        NSLog(@"Favorite data is:--> %@",ArrFav);
    }
}

wahy get data from database like " • " please note that my database description is text datatype ....am tried with varchar2 datatype also

but i want output like save data ..please help me out

thanks & regards

The initial string is UTF8.

You're recreating it from the db using this

[ArrFav addObject:[NSString stringWithFormat:@"%s",str]];

which is just converting str with naive ascii (the %s format specifier)

Try this instead to force it to use UTF-8 to decode the string :

[ArrFav addObject:[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding]];

Here's it the simplest solution, if you've only worried about symbole.

//Convert • symbole with some different symbole that SQLite DB allowing
- (NSString *) convertToDBFormat:(NSString *)str
{
     NSString *temp = [str stringByReplacingOccuranceOfStrings:@"•" withString:@"!~!"];
    return temp;
}

//Convert back to origional format after retrieving from SQLite DB
- (NSString *) convertBackMyFormat:(NSString *)str 
{
     NSString *temp = [str stringByReplacingOccuranceOfString:@"!~!" withString:@"•"];
    return temp;
}

Instead of !~! you can use any symbol which is not part of your string! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM