tamtoum1987 tamtoum1987 - 14 days ago 6
Objective-C Question

xcode sql characters with accent changed

When i save for example a string on database: "éric", when i retrieve it i get string like that √©ric, it's the same problem with other characters like à, è ...

I use sqlite3.h

Initialisation database:

- (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);
}
}


My file question.sql is on utf8, newStr has correct characters

And to retrieve data i do something like that

-(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;
}


any help will be welcomed thank's

--------------------------EDIT----------------------

The problem come from stringWithFormat

Instead of using this

question.question = [NSString stringWithFormat:@"%s",(char

*)sqlite3_column_text(statement, 1)];
I use stringWithUTF8String and now it's work
question.question = [NSString stringWithUTF8String:(char

*)sqlite3_column_text(statement, 1)];

Answer

The problem come from stringWithFormat

Instead of using this

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

I use stringWithUTF8String and now it's work

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