我在PostgreSQL数据库中有一个函数,它搜索服务器中的普通txt文件并处理文件中的数据。由于上传的文件格式(LATIN1
),SQL函数将客户端编码更改为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。
答案 0 :(得分:0)
如果要在不影响会话状态的情况下更改函数调用持续时间的print("choose two subjects")
print("English $100")
print("Math $75")
print("Science $125")
print("Stem $200")
a = input("Choose two subjects: ")
a = a.split()
for x in a:
x = x.lower()
print(x)
if x == "english":
eng = 100
elif x is not "english":
eng = int(0)
elif x == "science":
sci = 125
elif x is not "science":
sci = int(0)
elif x == "stem":
stem = 200
elif x is not "stem":
stem = int(0)
elif x == "math":
math = 100
elif x is not "math":
math = int(0)
print(eng + sci + math + stem)
设置,可以将client_encoding
子句附加到函数本身:
SET
虽然您的功能是通过CREATE FUNCTION import() RETURNS void
SET client_encoding = 'LATIN1'
AS ...
加载文件,但无需更改COPY
设置;您可以在client_encoding
命令本身中指定文件编码:
COPY