How to change character encoding of a PDO/SQLite connection in PHP?(如何在 PHP 中更改 PDO/SQLite 连接的字符编码?)
问题描述
我的 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 连接的字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHP 中更改 PDO/SQLite 连接的字符编码?
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
