如何在PHP中更改PDO/SQLite连接的字符编码? [英] How to change character encoding of a PDO/SQLite connection in PHP?

查看:98
本文介绍了如何在PHP中更改PDO/SQLite连接的字符编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不断运行非utf8字符串的php-gtk应用程序中遇到了一个小问题,我发现问题出在数据库连接中,即使数据库应该在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",但没有任何反应("

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

Also see: http://www.alberton.info/dbms_charset_settings_explained.html

这篇关于如何在PHP中更改PDO/SQLite连接的字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆