xcode sql 个重音字符已更改

xcode sql characters with accent changed

例如,当我在数据库中保存一个字符串:“éric”时,当我检索它时,我会得到类似 √©ric 的字符串,其他字符如 à、è ...

我用sqlite3.h

初始化数据库:

- (void) inistalizeDataBase{
NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory,    
NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *_databasePath = [[NSString alloc]
                           initWithString: [docsDir   
 stringByAppendingPathComponent:
                                            @"trueorfalse.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &ppDb) == SQLITE_OK)
    {




            char *errMsg;
            NSString *requeteEnString= [NSString 
  stringWithFormat:@"CREATE TABLE IF NOT EXISTS `%s` (`id` int(11) NOT    
 NULL,`questions` varchar(1024),`good_response`   
 varchar(1024),`difficulty` varchar(45),`response` 
 varchar(1024),`imagename` varchar(255),`asked` varchar(45),`type`  
  varchar(255) ,PRIMARY KEY (`id`))", "questions"];
    newStr




            const char *sql_stmt =[requeteEnString UTF8String];

            NSString *filePath = [[NSBundle mainBundle]    
  pathForResource:@"questions" ofType:@"sql"];
             NSData *htmlData = [NSData 
 dataWithContentsOfFile:filePath];
            NSString* newStr = [[NSString alloc] initWithData:htmlData 
 encoding:NSUTF8StringEncoding];

            const char *request = [newStr UTF8String];
            if (sqlite3_exec(ppDb, sql_stmt, NULL, NULL, &errMsg) ==    
 SQLITE_OK)
            {
                if (sqlite3_exec(ppDb, request, NULL, NULL, &errMsg) 
 == SQLITE_OK)
                {
                    NSLog(@"Creation Data Base succed, %s", 
 "questions");
                }
                else{
                    NSLog(@"echeeeeeec, %s", errMsg);

                }
            }
            else{
            NSLog(@"echeeeeeec, %s", errMsg);
            }





        //sqlite3_close(ppDb);
    } else {
        NSLog(@"Error when trying open Data Base");
    }

   }
    else{
    const char *dbpath = [_databasePath UTF8String];

    sqlite3_open(dbpath, &ppDb);
  }
 }

我的文件question.sql是utf8,newStr有正确的字符

为了检索数据,我做了类似的事情

  -(NSMutableArray*)retrieveListQuestions{

 NSMutableArray* listQuestionToReturn = [[NSMutableArray alloc] init];

  DataBaseHelper *dataBaseHelper = [DataBaseHelper    
 sharedDataBaseHelper];
[dataBaseHelper inistalizeDataBase];
sqlite3* ppDb = dataBaseHelper.ppDb;
NSString *query = [NSString stringWithFormat:@"SELECT   `id`,`questions`, `good_response`, `difficulty`, `response`, `imagename`, `asked`, `type` FROM %s where asked = 0","questions"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(ppDb, [query UTF8String], -1, &statement, nil)
    == SQLITE_OK) {

    while (sqlite3_step(statement) == SQLITE_ROW) {
        Questions *question =  [[Questions alloc]init];
        question.id = sqlite3_column_int(statement, 0);

 // HERE I HAVE WEIRD CHARACTERS
        question.question = [NSString stringWithFormat:@"%s",(char        
 *)sqlite3_column_text(statement, 1)];
        question.good_response = [NSString stringWithFormat:@"%s",       
 (char *)sqlite3_column_text(statement, 2)];
        question.difficulty = [NSString stringWithFormat:@"%s",(char 
   *)sqlite3_column_text(statement, 3)];
        question.response = [NSString stringWithFormat:@"%s",(char 
  *)sqlite3_column_text(statement, 4)];
        question.imagename = [NSString stringWithFormat:@"%s",(char 
  *)sqlite3_column_text(statement, 5)];
        question.asked = [NSString stringWithFormat:@"%s",(char 
  *)sqlite3_column_text(statement, 6)];
        question.type = [NSString stringWithFormat:@"%s",(char 
  *)sqlite3_column_text(statement, 7)];

        [listQuestionToReturn addObject:question];
    }
    sqlite3_finalize(statement);
}
return listQuestionToReturn;
}

欢迎任何帮助谢谢

------------------------编辑---------------- ----

问题出在stringWithFormat

而不是使用这个
question.question = [NSString stringWithFormat:@"%s",(char
*)sqlite3_column_text(语句, 1)]; 我使用 stringWithUTF8String 现在它可以工作了 question.question = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(语句, 1)];

问题出在stringWithFormat

而不是使用这个

question.question = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement, 1)];

我使用 stringWithUTF8String,现在可以使用了

question.question = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];