带重音的xcode sql字符已更改

时间:2016-01-06 14:44:33

标签: sql objective-c sqlite non-ascii-characters

当我在数据库中保存例如一个字符串:“é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;
}

欢迎任何帮助谢谢

-------------------------- EDIT -------------------- -

问题来自stringWithFormat

而不是使用这个        question.question = [NSString stringWithFormat:@“%s”,(char
       *)sqlite3_column_text(statement,1)]; 我使用stringWithUTF8String,现在它的工作         question.question = [NSString stringWithUTF8String :( char
      *)sqlite3_column_text(statement,1)];

1 个答案:

答案 0 :(得分:0)

问题来自stringWithFormat

而不是使用此

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

我使用stringWithUTF8String,现在它正在工作

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