[英]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.