沃梦达 / 编程问答 / php问题 / 正文

如何在 PHP 中更改 PDO/SQLite 连接的字符编码?

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

本文介绍了如何在 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 连接的字符编码?

基础教程推荐