带有重音的xcode sql字符已更改

Posted

技术标签:

【中文标题】带有重音的xcode sql字符已更改【英文标题】:xcode sql characters with accent changed 【发布时间】:2016-01-06 14:44:32 【问题描述】:

例如,当我在数据库中保存一个字符串:“é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(statement, 1)]; 我使用 stringWithUTF8String 现在它的工作 question.question = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];

【问题讨论】:

SQLite 不会以任何方式更改字符串的内容(除非您实际使用字符串函数)。您的程序在写入或读取数据时使用了错误的编码;你没有显示任何一个。 好的,但我不明白“你没有显示任何一个”是什么意思 感谢您发现问题而不是使用这个问题。question = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(statement, 1)];我使用它,现在它的工作:) question.question = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]; 【参考方案1】:

问题来自stringWithFormat

而不是使用这个

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

我用的是 stringWithUTF8String,现在可以了

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

【讨论】:

以上是关于带有重音的xcode sql字符已更改的主要内容,如果未能解决你的问题,请参考以下文章

在我的条件中搜索带有重音但没有重音的记录

ASP.NET Core 3.1 - IIS 服务器上带有重音字符问题的字符串后面的代码

MS SQL Server 中 JSON 函数路径中的重音字符导致错误

使用 xcode 重构的 NIB 名称未更改

无法在 iOS 13 上更改 Main.storyboard 的名称 [Xcode 11 GM 种子 2]

带有重音字符的 Java 属性文件