Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change character encoding of a PDO/SQLite connection in PHP?

Tags:

php

sqlite

pdo

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.

like image 571
levhita Avatar asked Nov 04 '08 19:11

levhita


People also ask

What is UTF-8 PHP?

The utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character.

Does PHP support UTF-8?

PHP UTF-8 Encoding – modifications to your code: 0, default_charset value is used as the default. From PHP 5.4. 0, UTF-8 was the default, but prior to PHP 5.4. 0, ISO-8859-1 was used as the default.

What encoding does SQLite use?

The default encoding will be UTF-8 for databases created using sqlite3_open() or sqlite3_open_v2(). The default encoding for databases created using sqlite3_open16() will be UTF-16 in the native byte order.

Does PDO work with SQLite?

The PDO_SQLITE extension provides the PDO driver for the SQLite 3 library. It supports standard PDO interfaces, and also custom methods for creating SQL functions and aggregates using PHP. In this section, we will walk you through the steps of using PDO to access SQLite databases.


1 Answers

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

like image 183
troelskn Avatar answered Nov 15 '22 15:11

troelskn