2

我是 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);
    }
}

如何从数据库中获取数据,如“,”,请注意我的数据库描述是文本数据类型....我也尝试使用 varchar2 数据类型

但我想要像保存数据这样的输出..请帮帮我

感谢和问候

4

2 回答 2

1

初始字符串是 UTF8。

您正在使用它从数据库中重新创建它

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

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

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

[ArrFav addObject:[[NSString alloc] initWithCString:str encoding:NSUTF8StringEncoding]];
于 2012-10-09T12:59:51.143 回答
0
于 2012-10-09T13:01:44.640 回答