問題描述
我的 php-gtk 應用程序遇到了一個小問題,它一直運行到非 utf8 字符串中,我發現問題出在數據庫連接上,即使數據庫應該是 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",但沒有任何反應(在SQLite 理解的查詢語言"頁面,所以我對此并不感到驚訝).
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:也許連接已經是 UTF-8 而數據不是,但如果有辦法改變連接編碼,這個問題仍然有用.
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.
推薦答案
據我所知,SQLite 只有一個字符集設置,這是在每個數據庫級別上的.您無法更改連接上的編碼.
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 有兩種不同的方式打開連接,可以是 UTF-8 或 UTF-16.我希望 PHP 的 SQLite 模塊(以及 PDO)簡單地使用 UTF-8 版本.如果這是正確的,我希望 SQLite 連接始終是 UTF-8.這意味著您應該使用 utf8_encode 手動編碼/解碼字符串/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.
另見:http://www.alberton.info/dbms_charset_settings_explained.html
這篇關于如何在 PHP 中更改 PDO/SQLite 連接的字符編碼?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!