I store the app data (cyrillic strings) in sqlite. When I try to display that data in the app I get strange characters instead of text. Here is how I get the data.
-(NSString *)getData
{
sqlite3 *database;
if(sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK)
{
sqlite3_close(database);
}
NSString *query = [NSString
stringWithFormat:@"SELECT name FROM users WHERE kind = '%@' ORDER BY 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 and not UTF-16 sqlite databases, you’re probably going to be happier using:
And, the more general:
can be used for other encodings that are NUL-safe. For example replace ENCODING with NSUTF16StringEncoding for UTF16 (and there are variants for BE and LE versions if you know ahead of time and can’t expect the marker to be there).
For encodings that are not NUL-terminated, you can use:
where ptr and length have the location and length of the string and ENCODING, as above, indicates the ENCODING from the list of available encodings.