[英]Encoding problem in sqlite and objective-c
我有一个内容如下的文件:
INSERT INTO table VALUES (NULL,'° F','Degrees Fahrenheit');
INSERT INTO table VALUES (NULL,'° C','Degrees Celsius');
现在,要解析这个,我有这样的东西:
NSString *sql = [NSString stringWithContentsOfFile:filename];
将此字符串打印到控制台看起来正确。 然后,我要用它做一个实际的插入语句:
const char *sqlString = [query UTF8String];
const char *endOfString;
if (sqlite3_prepare_v2(db, sqlString + nextStatementStart, -1, &stmt, &endOfString) != SQLITE_OK) {
return NO;
}
此时,检查查询仍会返回正确的结果。 也就是说, sqlite_sql(stmt)
返回INSERT INTO table VALUES (NULL,'° F','Degrees Fahrenheit');
所以我用sqlite3_step(stmt);
运行它
此时,查看数据库将显示以下内容:
1|° F|Degrees Fahrenheit
我不在任何地方使用任何sqlite_open16
函数(例如sqlite_open16
)。
编码问题在哪里? 我该如何解决?
从OSX 10.4开始不推荐使用stringWithContentsOfFile:
对于iPhone不确定),但是您想在这里使用stringWithContentsOfFile:encoding:error
指定编码
file
包含:
CREATE TABLE myvalues (foo TEXT, bar TEXT, baz TEXT);
INSERT INTO myvalues VALUES (NULL,'° F','Degrees Fahrenheit');
test.m包含(未提供错误处理):
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* s = [NSString stringWithContentsOfFile:@"file"
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"s: %@", s);
sqlite3* handle;
sqlite3_open("file.db", &handle);
sqlite3_exec(handle, [s UTF8String], NULL, NULL, NULL);
sqlite3_close(handle);
[pool release];
}
然后转储:
% sqlite3 file.db
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump
BEGIN TRANSACTION;
CREATE TABLE myvalues (foo TEXT, bar TEXT, baz TEXT);
INSERT INTO "myvalues" VALUES(NULL,'° F','Degrees Fahrenheit');
COMMIT;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.