如何使用 PostgreSQL 将字符串转换为 unicode? [英] How to convert string to unicode using PostgreSQL?

查看:542
本文介绍了如何使用 PostgreSQL 将字符串转换为 unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我想将我的字符串转换为 unicode.我正在使用 PostgreSQL 9.3 版本.在 SQL Server 中它更容易:

Here I want to convert my string to unicode. I am using PostgreSQL 9.3 version. In SQL Server its much more easy:

示例:

   sql = N'select * from tabletest'; /* For nvarchar/nchar/ntext */

   sql = U'select * from tabletest'; /* For varchar/char/text */

问题:如何在 PostgreSQL 中进行上述转换?

Question: How can I do the above conversion in PostgreSQL?

推荐答案

PostgreSQL 数据库具有本机字符类型,即服务器编码".通常是 utf-8.

PostgreSQL databases have a native character type, the "server encoding". It is usually utf-8.

所有文本都采用这种编码.不支持混合编码文本,除非存储为 bytea(即作为不透明的字节序列).

All text is in this encoding. Mixed encoding text is not supported, except if stored as bytea (i.e. as opaque byte sequences).

你不能存储unicode"或non-unicode"字符串,而且PostgreSQL没有varchar"和nvarchar"的概念.使用 utf-8,属于 7 位 ASCII 范围(和其他一些)的字符存储为单个字节,更宽的字符需要更多的存储空间,所以它只是自动的.对于全是宽"字符的文本,utf-8 比 ucs-2 或 utf-16 需要更多的存储空间,但对于混合文本则需要更少的存储空间.

You can't store "unicode" or "non-unicode" strings, and PostgreSQL has no concept of "varchar" vs "nvarchar". With utf-8, characters that fall in the 7-bit ASCII range (and some others) are stored as a single byte, and wider chars require more storage, so it's just automatic. utf-8 requires more storage than ucs-2 or utf-16 for text that is all "wide" characters, but less for text that's a mixture.

PostgreSQL 使用 client_encoding 设置自动转换为/从客户端的文本编码.无需显式转换.

PostgreSQL automatically converts to/from the client's text encoding, using the client_encoding setting. There is no need to convert explicitly.

如果您的客户端是Unicode"(Microsoft 产品在表示 UCS-2 或 UTF-16 时往往会说),那么大多数客户端驱动程序会处理任何 utf-8 <--> utf-16 转换你.

If your client is "Unicode" (which Microsoft products tend to say when they mean UCS-2 or UTF-16), then most client drivers take care of any utf-8 <--> utf-16 conversion for you.

所以你不需要关心,只要你的客户端使用正确的字符集选项进行 I/O 并设置一个正确的 client_encoding 匹配它在线路上实际发送的数据.(对于大多数客户端驱动程序,如 PgJDBC、nPgSQL 或 Unicode psqlODBC 驱动程序,这是自动的.

So you should not need to care, so long as your client does I/O with correct charset options and sets a correct client_encoding that matches the data its actually sends on the wire. (This is automatic with most client drivers like PgJDBC, nPgSQL, or the Unicode psqlODBC driver).

见:

这篇关于如何使用 PostgreSQL 将字符串转换为 unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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