编码 UTF8 的字符在 WIN1252 中没有等效项

发布于 2024-08-08 07:14:34 字数 260 浏览 2 评论 0原文

我收到以下异常:

Caused by: org.postgresql.util.PSQLException: ERROR: character 0xefbfbd of encoding "UTF8" has no equivalent in "WIN1252"

有没有办法通过 SQL 或以编程方式根除此类字符?
(SQL 解决方案应该是首选)。

我正在考虑使用WIN1252连接到数据库,但它会出现同样的问题。

I am getting the following exception:

Caused by: org.postgresql.util.PSQLException: ERROR: character 0xefbfbd of encoding "UTF8" has no equivalent in "WIN1252"

Is there a way to eradicate such characters, either via SQL or programmatically?
(SQL solution should be preferred).

I was thinking of connecting to the DB using WIN1252, but it will give the same problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

冷情 2024-08-15 07:14:34

我遇到了类似的问题,我通过在客户端中使用 \encoding UTF8 将编码设置为 UTF8 来解决,然后再尝试 INSERT INTO foo (SELECT * from bar WHERE x=y);< /代码>。我的客户端使用 WIN1252 编码,但数据库采用 UTF8,因此出现错误。

更多信息可在 PostgreSQL wiki 的 字符集支持(开发文档)下找到。

I had a similar issue, and I solved by setting the encoding to UTF8 with \encoding UTF8 in the client before attempting an INSERT INTO foo (SELECT * from bar WHERE x=y);. My client was using WIN1252 encoding but the database was in UTF8, hence the error.

More info is available on the PostgreSQL wiki under Character Set Support (devel docs).

一影成城 2024-08-15 07:14:34

当你收到这条消息时你会做什么?您是否将文件导入到 Postgres 中?正如 devstuff 所说,它是一个 BOM 字符。这是 Windows 在以 UTF8 编码保存时首先写入文本文件的字符 - 它是不可见的 0 宽度字符,因此在文本编辑器中打开它时您将看不到它。

尝试在记事本等中打开此文件,以 ANSI 编码另存为,然后在文件中添加(或替换类似的)set client_encoding to 'WIN1252' 行。

What do you do when you get this message? Do you import a file to Postgres? As devstuff said it is a BOM character. This is a character Windows writes as first to a text file, when it is saved in UTF8 encoding - it is invisible, 0-width character, so you'll not see it when opening it in a text editor.

Try to open this file in for example Notepad, save-as it in ANSI encoding and add (or replace similar) set client_encoding to 'WIN1252' line in your file.

被你宠の有点坏 2024-08-15 07:14:34

不要消灭这些角色,它们是真实的并且有充分的理由被使用。相反,消灭 Win1252。

Don't eridicate the characters, they're real and used for good reasons. Instead, eridicate Win1252.

将军与妓 2024-08-15 07:14:34

我有一个非常相似的问题。我有一个从 SQL Server 到 PostgreSQL 数据库的链接服务器。我使用 openquery 语句选择的表中的一些数据具有一些在 Win1252 中没有等效字符的字符。问题是我用于连接的系统 DSN 条目(可在 ODBC 数据源管理器下找到)被配置为使用 PostgreSQL ANSI(x64) 而不是 PostgreSQL Unicode(x64)。创建具有 Unicode 支持的新数据源并创建新的修改后的链接服务器并在 openquery 中引用新的链接服务器为我解决了这个问题。快乐的日子。

I had a very similar issue. I had a linked server from SQL Server to a PostgreSQL database. Some data I had in the table I was selecting from using an openquery statement had some character that didn't have an equivalent in Win1252. The problem was that the System DSN entry (to be found under the ODBC Data Source Administrator) I had used for the connection was configured to use PostgreSQL ANSI(x64) rather than PostgreSQL Unicode(x64). Creating a new data source with the Unicode support and creating a new modified linked server and refernecing the new linked server in your openquery resolved the issue for me. Happy days.

楠木可依 2024-08-15 07:14:34

我可以通过使用 Postgres 的子字符串函数并选择它来解决这个问题:

select substring(comments from 1 for 200) from billing

特殊字符开始每个字段的注释对于最终解决它有很大帮助。

I was able to get around it by using Postgres' substring function and selecting that instead:

select substring(comments from 1 for 200) from billing

The comment that the special character started each field was a great help in finally resolving it.

鸩远一方 2024-08-15 07:14:34

看起来字节序列 0xBD、0xBF、0xEF 是一个小端整数。这是 Unicode 字节顺序标记 (BOM) 字符 0xFEFF 的 UTF8 编码形式。

我不确定 Postgre 的正常行为是什么,但 BOM 通常仅用于输入流开头的编码检测,并且通常不会作为结果的一部分返回。

无论如何,您的异常是由于此代码点在 Win1252 代码页中没有映射。大多数其他非拉丁字符也会发生这种情况,例如亚洲文字中使用的字符。

您可以将数据库编码更改为 UTF8 而不是 1252 吗?这将允许您的列包含几乎任何字符。

That looks like the byte sequence 0xBD, 0xBF, 0xEF as a little-endian integer. This is the UTF8-encoded form of the Unicode byte-order-mark (BOM) character 0xFEFF.

I'm not sure what Postgre's normal behaviour is, but the BOM is normally used only for encoding detection at the beginning of an input stream, and is usually not returned as part of the result.

In any case, your exception is due to this code point not having a mapping in the Win1252 code page. This will occur with most other non-Latin characters too, such as those used in Asian scripts.

Can you change the database encoding to be UTF8 instead of 1252? This will allow your columns to contain almost any character.

最丧也最甜 2024-08-15 07:14:34

这个问题是在 2016 年 11 月 19 日左右出现的,当时我们的旧 Access 97 应用程序正在访问 postgresql 9.1 数据库。

通过将驱动程序更改为 UNICODE 而不是 ANSI 解决了这个问题(请参阅 plang 注释)。

This problem appeared for us around 19/11/2016 with our old Access 97 app accessing a postgresql 9.1 DB.

This was solved by changing the driver to UNICODE instead of ANSI (see plang comment).

老旧海报 2024-08-15 07:14:34

这对我有用:
1 在 sp_configure 中启用即席查询。
2 为链接的 PostgreSQL 服务器添加 ODBC DSN。
3 确保您有 ANSI 和 Unicode (x64) 驱动程序(尝试同时使用)。
4 运行如下查询 - 更改 UID、服务器 ip、数据库名称和密码。
5 只需将查询保留在 postgreSQL 格式的最后一行。

EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO

SELECT * FROM OPENROWSET('MSDASQL', 
'Driver=PostgreSQL Unicode(x64); 
uid=loginid;
Server=1.2.3.41;
port=5432;
database=dbname;
pwd=password',

'select * FROM table_name limit 10;')

Here's what worked for me :
1 enable ad-hoc queries in sp_configure.
2 add ODBC DSN for your linked PostgreSQL server.
3 make sure you have both ANSI and Unicode (x64) drivers (try with both).
4 run query like this below - change UID, server ip, db name and password.
5 just keep the query in last line in postgreSQL format.

EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO

SELECT * FROM OPENROWSET('MSDASQL', 
'Driver=PostgreSQL Unicode(x64); 
uid=loginid;
Server=1.2.3.41;
port=5432;
database=dbname;
pwd=password',

'select * FROM table_name limit 10;')
∞觅青森が 2024-08-15 07:14:34

当我的 Windows 10 使用普通话作为默认语言时,我遇到了这个问题。出现此问题是因为我确实尝试使用 UTF-8 导入数据库。通过 psql 检查并执行“\l”,它显示 collat​​e 和 cytpe 是普通话中国。

解决方案,将操作系统语言重置回美国并重新安装 PostgreSQL。当整理回 UTF-8 时,您可以再次重置操作系统语言。

我在这里写了完整的上下文和解决方案 https:/ /www.yodiw.com/fix-utf8-encoding-win1252-cputf8-postgresql-windows-10/

I have face this issue when my Windows 10 using Mandarin China as default language. This problem has occurred because I did try to import a database with UTF-8. Checking via psql and do "\l", it shows collate and cytpe is Mandarin China.

The solution, reset OS language back to US and re-install PostgreSQL. As the collate back to UTF-8, you can reset back your OS language again.

I write the full context and solution here https://www.yodiw.com/fix-utf8-encoding-win1252-cputf8-postgresql-windows-10/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文