irpas技术客

【Postgresql】ERROR: invalid byte sequence for encoding “UTF8“: 0x00_Shaula10

网络投稿 2340

目录 1.问题描述2.结论3.解决方法4.解决过程

1.问题描述

最近在做Oracle到Postgresql表数据的同步,采用的是Kettle表输入表输出组件,其中几张表同步时总是报如下错误: ERROR Batch entry 0 INSERT INTO [table_name] ([column_name]) VALUES ([values]) was aborted: ERROR: invalid byte sequence for encoding “UTF8”: 0x00 Call getNextException to see other errors in the batch.

2.结论

数据中存在 chr(0) 字符 在oracle中, chr(0) 表示的是字符串的结束符 而pg中可能不支持这种字符。

3.解决方法

使用 replace([column_name],chr(0),’’) 或者 replace([column_name],chr(0),null) 以上两种都可,视情况而定

如果不确定哪个字段含有chr(0),可以这样找:

select case when [column_name1] like '%'||chr(0)||'%' then 1 end as [column_name1] ,case when [column_name2] like '%'||chr(0)||'%' then 1 end as [column_name2] ,... from [table_name] where [column_name1] like '%'||chr(0)||'%' or [column_name2] like '%'||chr(0)||'%' or ... ;
4.解决过程

起初以为是pg库不支持oracle的gbk字符,在百度上找了个oracle字符集转换函数convert(),试了一下,还是报同样的错误。

我试着在pg库中,执行错误日志中的INSERT INTO语句,发现执行成功。

在oracle库中找到这条数据,发现其中有个字段数据看起来有些不一样。 正常情况下,plsql结果集中NULL值,是黄底空白显示,而这个字段是白底空白显示 说明该字段中有记录数据,而非NULL值。

我试着执行以下sql:

select [column_name] ,case when [column_name] = '' then 1 when [column_name] = ' ' then 2 when [column_name] is null then 3 end from [table_name];

但结果都不符合以上条件 试着对其转义:

select ascii([column_name]) from [table_name];

结果为0,似乎是 chr(0) 对应ASCII编码表中的 NUL (NULL),但解释为空字符,在这里有些说不通

同事提醒我可能是换行符,于是:

select [column_name] ,case [column_name] when chr(10) then 10 when chr(13) then 13 when chr(32) then 32 end from [table_name];

结果依然不符合以上条件 于是:

select [column_name] ,case [column_name] when chr(0) then 0 when chr(1) then 1 when chr(2) then 2 when chr(3) then 3 when chr(4) then 4 when chr(5) then 5 when chr(6) then 6 when chr(7) then 7 when chr(8) then 8 when chr(9) then 9 when chr(10) then 10 when chr(11) then 11 when chr(12) then 12 when chr(13) then 13 when chr(14) then 14 when chr(15) then 15 when chr(16) then 16 when chr(17) then 17 when chr(18) then 18 when chr(19) then 19 when chr(20) then 20 when chr(21) then 21 when chr(22) then 22 when chr(23) then 23 when chr(24) then 24 when chr(25) then 25 when chr(26) then 26 when chr(27) then 27 when chr(28) then 28 when chr(29) then 29 when chr(30) then 30 when chr(31) then 31 when chr(32) then 32 when chr(33) then 33 when chr(34) then 34 when chr(35) then 35 when chr(36) then 36 when chr(37) then 37 when chr(38) then 38 when chr(39) then 39 when chr(40) then 40 when chr(41) then 41 when chr(42) then 42 when chr(43) then 43 when chr(44) then 44 when chr(45) then 45 when chr(46) then 46 when chr(47) then 47 end from [table_name];

结果依然是chr(0) 最终找到一篇帖子chr(0)的陷阱,里面解释道: 在oracle里面chr(0)表示的是字符串的结束符

这样便说的通了。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #PostgresqlERROR #invalid #byte #sequence #for #encoding #UTF8 #0x00