SQLite MYSQL character encoding

早过忘川 提交于 2019-12-11 07:02:04

问题


I have a strange situation where the following code works however XCode warns it is deprecated...

NSString *col1 = [NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 0)];

However as that is the deprecated method if I set an encoding the string comes out wrong! I have tried all the encodings but none work!

NSString *col1 = [NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 0) encoding:NSASCIIStringEncoding];


回答1:


I would have expected satire's suggestion to have worked, however here is the Cocoa version of what I use:

If you are dealing with utf16

int len = sqlite3_column_bytes16(compiledStatement, col) / 2;
unichar *chars = (unichar*)sqlite3_column_text16(compiledStatement, col);
NSString *str = [NSString stringWithCharacters:chars length:len];

If you are using utf8

int len = sqlite3_column_bytes(compiledStatement, col);
char *chars = (char*)sqlite3_column_text(compiledStatement, col);
NSString *str = [NSString stringWithUTF8String:chars length:len];



回答2:


try NSString *col1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];

if your database coded with utf8




回答3:


First check that entry in the database contaoins Same Roman Words or not .

NSString *strtemp = [NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 0)]; strtemp=[strtemp stringByAddingPercentEscapesUsingEncoding:NSMacOSRomanStringEncoding];

NSMacOSRomanStringEncoding will definately work.I have face same issue.




回答4:


Try this it works for me for UTF8 encoding

char *chars = (char*)sqlite3_column_text(compiledStatement, col);
NSString *str = [NSString stringWithCString:chars encoding:NSUTF8StringEncoding];


来源:https://stackoverflow.com/questions/5258007/sqlite-mysql-character-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!