Postgres:使用psycopg2或附近未终止的引用字符串

Posted

技术标签:

【中文标题】Postgres:使用psycopg2或附近未终止的引用字符串【英文标题】:Postgres: unterminated quoted string at or near using psycopg2 【发布时间】:2017-02-16 21:24:27 【问题描述】:

我有一个包含两个表 stream_239_1_1_1_1234_0stream_239_0_0_40_1234_0 的 postgres 数据库。每个表都有一个名为alarm 的列。我正在使用psycopg2 Python 客户端将数据保存到这些表中。

将数据插入stream_239_1_1_1_1234_0 没有问题,如下所示:

INSERT INTO stream_239_1_1_1_1234_0 (alarm) VALUES ('"delay_max":
0.0, "ts_errors": ["count": 0, "state": 0, "is_priority1": true, "name": "SYNC", "is_priority2": false, "count": 0, "state": 0,
"is_priority1": true, "name": "BYTE", "is_priority2": false,
"count": 0, "state": 0, "is_priority1": true, "name": "PAT",
"is_priority2": false, "count": 0, "state": 0, "is_priority1": true,
"name": "CC", "is_priority2": false, "count": 0, "state": 0,
"is_priority1": true, "name": "PMT", "is_priority2": false, "count":
0, "state": 0, "is_priority1": true, "name": "PID", "is_priority2":
false, "count": 0, "state": 0, "is_priority1": false, "name": "TS",
"is_priority2": true, "count": 0, "state": 0, "is_priority1": false,
"name": "CRC", "is_priority2": true, "count": 0, "state": 0,
"is_priority1": false, "name": "PCR", "is_priority2": true, "count":
0, "state": 0, "is_priority1": false, "name": "ACC", "is_priority2":
true, "count": 0, "state": 0, "is_priority1": false, "name": "PTS",
"is_priority2": true, "count": 0, "state": 0, "is_priority1": false,
"name": "CAT", "is_priority2": true], "is_stream_paused": false,
"delay_min": 0.0, "ac_err": 0.0, "oj_err":
0.0,"ipm_errors":["mnv":-3017, "mxv":2996, "mev":0, "mi":1000, "cr":0, "Bps":276667.104687, "df":4919, "pkl":0, "oos":0, "rei":0,
"reo":0, "ncl":0.0, "slf":0.0, "mtl":0.0, "ld":0, "lpl":0, "lr":0.0],
"timestamp": "2017:02:16:16:10:49","id3_data": "None"');

而我在此查询中遇到此错误

INSERT INTO stream_239_0_0_40_1234_0 (alarm) VALUES ('"delay_max":
0.0, "ts_errors": ["count": 0, "state": 0, "is_priority1": true, "name": "SYNC", "is_priority2": false, "count": 0, "state": 0,
"is_priority1": true, "name": "BYTE", "is_priority2": false,
"count": 0, "state": 0, "is_priority1": true, "name": "PAT",
"is_priority2": false, "count": 0, "state": 0, "is_priority1": true,
"name": "CC", "is_priority2": false, "count": 0, "state": 0,
"is_priority1": true, "name": "PMT", "is_priority2": false, "count":
0, "state": 0, "is_priority1": true, "name": "PID", "is_priority2":
false, "count": 0, "state": 0, "is_priority1": false, "name": "TS",
"is_priority2": true, "count": 0, "state": 0, "is_priority1": false,
"name": "CRC", "is_priority2": true, "count": 0, "state": 0,
"is_priority1": false, "name": "PCR", "is_priority2": true, "count":
0, "state": 0, "is_priority1": false, "name": "ACC", "is_priority2":
true, "count": 0, "state": 0, "is_priority1": false, "name": "PTS",
"is_priority2": true, "count": 0, "state": 0, "is_priority1": false,
"name": "CAT", "is_priority2": true], "is_stream_paused": false,
"delay_min": 0.0, "ac_err": 0.0, "oj_err":
0.0,"ipm_errors":["mnv":-140, "mxv":143, "mev":0, "mi":1000, "cr":0, "Bps":697856.570014, "df":0, "pkl":0, "oos":0, "rei":0, "reo":0,
"ncl":0.0, "slf":0.0, "mtl":0.0, "ld":0, "lpl":0, "lr":0.0],
"timestamp": "2017:02:16:16:16:43","id3_data": ["Payload":
"2016:05:12T11:47:41:000z", "id3_timestamp":
"2017:02:16:21:16:40"]');

错误是:

在 "'"delay_max": 0.0 处或附近未终止的带引号的字符串, "ts_errors": ["count": 0, "state": 0, "is_priority1": true, "name": "SYNC", "is_priority2": false, "count": 0, "state": 0, “is_priority1”:真,“名称”:“字节”,“is_priority2”:假, “计数”:0,“状态”:0,“is_priority1”:真,“名称”:“PAT”, "is_priority2": false, "count": 0, "state": 0, "is_priority1": true, “名称”:“CC”,“is_priority2”:假,“计数”:0,“状态”:0, “is_priority1”:真,“名称”:“PMT”,“is_priority2”:假,“计数”: 0,“状态”:0,“is_priority1”:真,“名称”:“PID”,“is_priority2”: 假,“计数”:0,“状态”:0,“is_priority1”:假,“名称”:“TS”, "is_priority2": true, "count": 0, "state": 0, "is_priority1": false, “名称”:“CRC”,“is_priority2”:true,“计数”:0,“状态”:0, “is_priority1”:假,“名称”:“PCR”,“is_priority2”:真,“计数”: 0,“状态”:0,“is_priority1”:假,“名称”:“ACC”,“is_priority2”: true,“count”:0,“state”:0,“is_priority1”:false,“name”:“PTS”, "is_priority2": true, "count": 0, "state": 0, "is_priority1": false, “名称”:“CAT”,“is_priority2”:true],“is_stream_paused”:false, “delay_min”:0.0,“ac_err”:0.0,“oj_err”: 0.0,“ipm_errors”:[“mnv”:-140,“mxv”:143,“mev”:0,“mi”:1000,“cr”:0,“Bps”:697856.570014,“df”:0 , "pkl":0, "oos":0, "rei":0, "reo":0, “ncl”:0.0,“slf”:0.0,“mtl”:0.0,“ld”:0,“lpl”:0,“lr”:0.0], “时间戳”:“2017:02:16:16:16:43”,“id3_data”:[“有效载荷”: “2016:05:12T11:47:41:000z”第 1 行:...ERT INTO stream_239_0_0_40_1234_0(警报)值('"delay_m...

如果我尝试从 postgres shell 手动运行查询,它工作正常! 我查看了this question,但我不确定它与我的情况有什么关系。 psycopg2 Python 客户端已经可以很好地处理第一个查询!

有什么建议吗?

编辑 "ac_err": 0.0, "oj_err": 0.0, "id3_data": ["Payload": "2016:05:12T11:47:41:000z", "id3_timestamp": "2017:02: 16:21:16:40"], "delay_max": 0.0, "delay_min": 0.0, "timestamp": "2017:02:16:16:16:43", "ts_errors": ["name" :“SYNC”,“count”:0,“state”:0,“is_priority1”:true,“is_priority2”:false,“name”:“BYTE”,“count”:0,“state”:0 , "is_priority1": true, "is_priority2": false, "name": "PAT", "count": 0, "state": 0, "is_priority1": true, "is_priority2": false, "名称”:“CC”,“计数”:0,“状态”:0,“is_priority1”:真,“is_priority2”:假,“名称”:“PMT”,“计数”:0,“状态” :0,“is_priority1”:真,“is_priority2”:假,“名称”:“PID”,“计数”:0,“状态”:0,“is_priority1”:真,“is_priority2”:假, “name”:“TS”,“count”:0,“state”:0,“is_priority1”:false,“is_priority2”:true,“name”:“CRC”,“count”:0,“状态”:0,“is_priority1”:假,“is_priority2”:真,“名称”:“PCR”,“计数”:0,“状态”:0,“is_priority1”:假,“is_priority2”:真, “姓名": "ACC", "count": 0, "state": 0, "is_priority1": false, "is_priority2": true, "name": "PTS", "count": 0, "state": 0,“is_priority1”:假,“is_priority2”:真,“名称”:“CAT”,“计数”:0,“状态”:0,“is_priority1”:假,“is_priority2”:真], “ipm_errors”:[“cr”:0,“df”:0,“ld”:0,“lr”:0.0,“mi”:1000,“Bps”:697856.570014,“lpl”:0,“mev” “:0,“mnv”:-140,“mtl”:0.0,“mxv”:143,“ncl”:0.0,“oos”:0,“pkl”:0,“rei”:0,“reo” : 0, "slf": 0.0], "is_stream_paused": false

编辑我找到了答案,我会发布它

【问题讨论】:

根据JSONLint是一个有效的JSON文本。 【参考方案1】:

问题基本上在于“Payload”中形成 JSON 的字符串的编码:“2016:05:12T11:47:41:000z”。

此有效负载来自 C++ 函数,并被传递到 Python 端。它代表分组数据。出于某种原因,有效载荷有一个奇怪的编码。尽管我确保 Payload 是字符串格式并且对象是有效的 JSON 格式,但 psycopg2 无法识别这种编码,这证明了我能够手动运行查询的原因。我通过删除有效负载数据末尾的 Null 终止字符来解决问题

【讨论】:

以上是关于Postgres:使用psycopg2或附近未终止的引用字符串的主要内容,如果未能解决你的问题,请参考以下文章

中断键时使用 psycopg2 终止未完成的 SQL 提交查询

带有“%%”参数的 Postgres 查询未通过 psycopg2 返回结果

错误:在“$BODY$”处或附近未终止的美元引号字符串

psycopg2.ProgrammingError:“st”或附近的语法错误\r,

使用 psycopg2 在 python 中执行查询时出现“ProgrammingError:在或附近出现语法错误”

创建函数错误错误:在“$$”处或附近未终止的美元引号字符串