objective c - Broken encoding while fetching data from sqlite database -


i store app data (cyrillic strings) in sqlite. when try display data in app strange characters instead of text. here how data.

-(nsstring *)getdata {     sqlite3 *database;      if(sqlite3_open([[self datafilepath] utf8string], &database) != sqlite_ok)     {         sqlite3_close(database);     }      nsstring *query = [nsstring                         stringwithformat:@"select name users kind = '%@' order random() limit 1", self.kind ];     sqlite3_stmt *statement;      nsstring *selectedquestion;      if(sqlite3_prepare_v2(database, [query utf8string], -1, &statement, nil) == sqlite_ok)     {         sqlite3_step(statement);         selectedquestion =[nsstring stringwithformat: @"%s",(char *)sqlite3_column_text(statement, 0)];         sqlite3_finalize(statement);     }      sqlite3_close(database);      return selectedquestion; } 

assuming you're using utf-8 , not utf-16 sqlite databases, you're going happier using:

selectedquestion = [nsstring stringwithutf8string: (char*)sqlite3_column_text(statement,0)]; 

and, more general:

selectedquestion = [nsstring stringwithcstring: (char*) sqlite3_column_text(statement,0) encoding: encoding]; 

can used other encodings nul-safe. example replace encoding nsutf16stringencoding utf16 (and there variants , le versions if know ahead of time , can't expect marker there).

for encodings not nul-terminated, can use:

selectedquestion = [[[nsstring alloc] initwithbytes: ptr  length: length encoding: encoding] autorelease]; 

where ptr , length have location , length of string , encoding, above, indicates encoding list of available encodings.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -