简体   繁体   English

在iPhone中插入特殊字符的数据库问题?

[英]database issues for insert special character in iphone?

i am new to sqlite database & iphone developement 我是sqlite数据库和iPhone开发的新手

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 获取favirote列表代码为

-(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 为什么要从数据库中获取数据,例如“,Ä¢”,请注意,我的数据库描述是文本数据类型.... am也尝试使用varchar2数据类型

but i want output like save data ..please help me out 但是我想输出像保存数据..请帮帮我

thanks & regards 感谢和问候

The initial string is UTF8. 初始字符串为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) 这只是用朴素的ascii( %s格式说明符)转换str

Try this instead to force it to use UTF-8 to decode the string : 尝试使用此方法强制其使用UTF-8解码字符串:

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

Here's it the simplest solution, if you've only worried about symbole. 下面是它的简单的解决方案,如果你只担心 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! 您可以使用不属于字符串的任何符号! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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