SQLite MYSQL character encoding
I have a strange situation where the following code works however XCode warns it is deprecated...
NSString *col1 = [NSString stringWithCString:(char *)sqlite3_colum开发者_如何学运维n_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];
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];
try NSString *col1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
if your database coded with utf8
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.
Try this it works for me for UTF8 encoding
char *chars = (char*)sqlite3_column_text(compiledStatement, col);
NSString *str = [NSString stringWithCString:chars encoding:NSUTF8StringEncoding];
精彩评论