我找不到解决方案。我将数据存储到本地 SQLITE DB 中。一切正常,除了重音词。如图所示。
但是,如果我尝试使用 SqliteManager(Firefox 插件)存储重音单词,一切正常。总之:当我使用我的应用程序存储重音单词时,会出现奇怪的字符。我使用以下代码写入数据(读取相同)。基本上,所有字符串都是 UTF8 编码的。
-(NSInteger)writeData:(NSDictionary* )data table:(NSString* )table
{
sqlite3_stmt *sqlStatement;
NSMutableArray *columns = [[NSMutableArray alloc] init];
NSMutableArray *values = [[NSMutableArray alloc] init];
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithDictionary:data];
@try {
assert([data count] != 0);
if ([[data allKeys] count] == 0) return 1;
[temp removeObjectForKey:@"id"];
[columns addObjectsFromArray:[temp allKeys]];
NSString *cols = [columns componentsJoinedByString:@","];
NSMutableString *colNames = [[NSMutableString alloc] initWithString:
[NSString stringWithFormat:@"INSERT INTO %s (",[table UTF8String]]];
[colNames appendString:cols];
[colNames appendString:@")"];
// VALUES FOR INSERT
[values addObjectsFromArray:[temp allValues] ];
NSMutableString *s = [[NSMutableString alloc] init];
for(int i = 0; i < [values count]; i++)
{
[s setString:[NSString stringWithFormat:@"%@",[values objectAtIndex:i]]];
const char* currentValue = [s UTF8String];
[values setObject:[NSString stringWithFormat:@"\"%s\"",currentValue] atIndexedSubscript:i];
}
NSString *vals = [values componentsJoinedByString:@","];
NSMutableString *valNames = [[NSMutableString alloc] initWithString:@" VALUES ("];
[valNames appendString:vals];
[valNames appendString:@")"];
[colNames appendString:valNames];
const char *sql = [colNames UTF8String];
#ifdef DEBUG
NSLog(@"avvDB writeDATA insert string %@",colNames);
#endif
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement write %s",sqlite3_errmsg(db));
return 0;
}
if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK)
{
// NSLog(@"Last id %llu %s",sqlite3_last_insert_rowid(db),sqlite3_errmsg(db));
}
}// end try
@catch(NSException* e)
{
NSLog(@"Eccezione in write %@",[e reason]);
}
@finally {
sqlite3_reset(sqlStatement);
sqlite3_finalize(sqlStatement);
sqlStatement = nil;
return sqlite3_last_insert_rowid(db);
}
}