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

时间:2012-10-09 12:55:06

标签: iphone database sqlite

我是sqlite数据库的新手iphone开发

将数据作为

插入数据库
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);    
}

我将上述数据插入数据库但问题是

当我从数据库显示保存的数据时,它显示为

• 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

将首选列表代码设为

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

从数据库获取数据,如“,Ä¢” 请注意我的数据库描述是text数据类型....我也尝试使用varchar2数据类型

但我希望输出像保存数据..请帮助我

谢谢&问候

2 个答案:

答案 0 :(得分:1)

初始字符串是UTF8。

您正在使用此

从数据库重新创建它
[ArrFav addObject:[NSString stringWithFormat:@"%s",str]];

只是用天真的ascii(str格式说明符)转换%s

尝试使用它来强制它使用UTF-8解码字符串:

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

答案 1 :(得分:0)

如果您只担心符号,这是最简单的解决方案。

//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;
}

您可以使用任何不属于字符串的符号代替!~!! :)