我找不到解决方案。 我将数据存储到本地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);
}
}
答案 0 :(得分:2)
%s
运算符不支持unicode字符。正如字符串编程指南的String Format Specifiers所述,它是“8位无符号字符的空终止数组。”
坦率地说,由于其他原因,你不应该使用stringWithFormat
(如果其中一个字符串中有引号,那么......你的SQL语句会失败;你甚至会暴露给SQL注射攻击)。您应该使用?
占位符(不带引号),然后为要绑定到相应问号的每个参数调用sqlite3_bind_text
(注意,sqlite3_bind_xxx
个函数使用基于1的索引,与使用基于0的索引的sqlite3_column_xxx
不同。