問(wèn)題描述
我的 php-gtk 應(yīng)用程序遇到了一個(gè)小問(wèn)題,它一直運(yùn)行到非 utf8 字符串中,我發(fā)現(xiàn)問(wèn)題出在數(shù)據(jù)庫(kù)連接上,即使數(shù)據(jù)庫(kù)應(yīng)該是 UTF-8.
I'm having a little problem with a php-gtk app that keeps running into non-utf8 strings, I had found that the problem is in the database connection, even when the database is supposed to be in UTF-8.
我曾嘗試使用SET CHARACTER SET utf8"(MySQL 方式)和SET NAMES UTF8",但沒(méi)有任何反應(yīng)(在SQLite 理解的查詢語(yǔ)言"頁(yè)面,所以我對(duì)此并不感到驚訝).
I had tried with the "SET CHARACTER SET utf8"(MySQL way) and the "SET NAMES UTF8" and nothing happen (there isn't any information about none of this commands in the "Query Language Understood by SQLite " page either, so I'm not surprised about that).
PD:也許連接已經(jīng)是 UTF-8 而數(shù)據(jù)不是,但如果有辦法改變連接編碼,這個(gè)問(wèn)題仍然有用.
PD: Maybe the connection is already in UTF-8 and the data isn't, but if there is a way to change the connection encoding this question would still be useful.
推薦答案
據(jù)我所知,SQLite 只有一個(gè)字符集設(shè)置,這是在每個(gè)數(shù)據(jù)庫(kù)級(jí)別上的.您無(wú)法更改連接上的編碼.
As far as I can tell, SQLite only has one setting for charset, which is on a per-database level. You can't change the encoding on the connection.
C API 有兩種不同的方式打開(kāi)連接,可以是 UTF-8 或 UTF-16.我希望 PHP 的 SQLite 模塊(以及 PDO)簡(jiǎn)單地使用 UTF-8 版本.如果這是正確的,我希望 SQLite 連接始終是 UTF-8.這意味著您應(yīng)該使用 utf8_encode 手動(dòng)編碼/解碼字符串/utf8_decode.
The C API has two different ways of opening a connection, either as UTF-8 or UTF-16. I would expect PHP's SQLite module (And thus PDO) to simply use the UTF-8 version. If that's correct, I would expect that a SQLite connection is always UTF-8. This means that you ought to manually encode/decode strings with utf8_encode/utf8_decode.
另見(jiàn):http://www.alberton.info/dbms_charset_settings_explained.html
這篇關(guān)于如何在 PHP 中更改 PDO/SQLite 連接的字符編碼?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!