SQLite Forum

Database UTF-16 support

Database UTF-16 support

(1) By anonymous on 2021-03-01 08:59:13 [link] [source]

Thus, SQLite support UTF-16 databases?

(2) By Simon Slavin (slavin) on 2021-03-01 12:37:30 in reply to 1 [link] [source]

The API for SQLite3 contains routines that accept text as both UTF-8 and UTF-16 in the native byte order of the host machine. Each database file manages text as either UTF-8, UTF-16BE (big-endian), or UTF-16LE (little-endian). You can let SQLite choose a format suited to your setup, or you can specify one yourself:


SQLite API calls will accept whatever form you specify and do any needed conversion internally. You can, of course, speed this up by being consistent in the form you use, which reduces the conversions needed.

(3) By anonymous on 2021-03-01 12:54:00 in reply to 2 [link] [source]

That's nice. So I have read that one can do this. The big question is how to accomplish this.

Let's begin with a Heads off for writing the Unicode functions in C.

So I want to query something and use this API call: rc = sqlite3_exec(DB, command, exec_result, 0, &ErrMessage);

DB is of course a pointer. assume the command is a valid SQL-statement using UTF-16. Will this work? The callback is execute_result.

The UTF-8 callback looks like this: exec_result(void *unused, const int columns, char** data, char** column_name)

So how do I define a UTF-16 callback? char16** data e.g.?

By the way is the int 32 bit or 64 bit?

(5) By Keith Medcalf (kmedcalf) on 2021-03-01 13:34:48 in reply to 3 [link] [source]

sqlite3_exec does not do UTF-16, either for the SQL (command) or for the results.

As a primitive interface it only does UTF-8.

If you wish to use UTF-16 then you need to use the full prepare/bind/exec/column interfaces that have UTF-16 versions.

(4) By Keith Medcalf (kmedcalf) on 2021-03-01 13:29:50 in reply to 2 [source]

You can let SQLite choose a format suited to your setup

Actually no. Left to it's own devices SQLite3 will, unless you set the default differently at compile time, default to UTF-8 encoding for the database encoding.

You may specify the encoding to be used for text data stored in the database using pragma encoding as documented.

The encoding of the "internal data" stored by the database (the database encoding) is quite independent of the format used to send or retrieve data between SQLite3 and your application, and format conversions will be carried out between encodings as needed.