如何更改 PHP 中 PDO/SQLite 连接的字符编码?
我的 php-gtk 应用程序遇到了一个小问题,它不断运行到非 utf8 字符串,我发现问题出在数据库连接中,即使数据库应该是 UTF-8 的。
我尝试过“SET CHARACTER SET utf8”(MySQL方式)和“SET NAMES UTF8”,但没有任何反应(在“SQLite 理解的查询语言 ”页面,所以我对此并不感到惊讶)。PD
:也许连接已经是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.
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: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知, SQLite 只有一种字符集设置,它位于每个数据库级别。 您无法更改连接上的编码。
C API 有两种不同的方式打开连接,无论是 UTF-8 还是 UTF -16。 我希望 PHP 的 SQLite 模块(以及 PDO)只使用 UTF-8 版本。 如果这是正确的,我希望 SQLite 连接始终是 UTF-8。 这意味着您应该使用 utf8_encode 手动编码/解码字符串/utf8_decode。
另请参阅:
http://www.alberton.info/dbms_charset_settings_explained.html
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.
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.
Also see:
http://www.alberton.info/dbms_charset_settings_explained.html