我在 PostgreSQL 数据库中有一个函数,它在服务器中搜索一个普通的 txt 文件并处理文件中的数据。SQL 函数将客户端编码更改为LATIN1
由于上传的文件格式 ( SET client_encoding ='LATIN1';
)。当我在本地(使用 PgAdmin)执行该函数时,它可以工作,但是当我使用 JPA Native Query 执行该函数时,出现此错误:
Internal Exception: org.postgresql.util.PSQLException: The server's client_encoding parameter was changed to LATIN1. The JDBC driver requires client_encoding to be UTF8 for correct operation.
这是我的 JPA 函数:
public boolean importFile() {
boolean res = false;
try {
getEntityManager().getTransaction().begin();
getEntityManager().createNativeQuery("SELECT import()").executeUpdate();
getEntityManager().getTransaction().commit();
res = true;
} catch (Exception e) {
e.printStackTrace();
getEntityManager().getTransaction().rollback();
System.err.println("Error: " + e.getMessage());
}
return res;
}
如果重要的话,我正在使用 EclipseLink(JPA 2.1)、JSF 2.2 (PrimeFaces 6.1)、PostgreSQL 9.5.8。
如果要在client_encoding
函数调用期间更改设置而不影响会话状态,可以SET
在函数本身附加一个子句:
CREATE FUNCTION import() RETURNS void
SET client_encoding = 'LATIN1'
AS ...
虽然如果您的函数是通过 加载文件COPY
,则无需更改您的client_encoding
设置;您可以在COPY
命令本身中指定文件编码:
COPY my_table
FROM '/path/to/my_file'
ENCODING 'LATIN1'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句