如何使用 PostgreSQL 将字符串转换为 unicode?

编程入门 行业动态 更新时间:2024-10-19 23:48:53
本文介绍了如何使用 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?

本文发布于:2023-10-27 11:12:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   如何使用   unicode   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!