Postgres:使用psycopg2

时间:2017-02-16 21:24:27

标签: python json postgresql

我有一个包含两个表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”: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”第1行:...... ERT INTO   stream_239_0_0_40_1234_0(闹钟)VALUES('{“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},{“ name“:”CC“,”count“:0,”state“:0,”is_priority1“:true,”is_priority2“:false},{”name“:”PMT“,”count“:0,”state“ :0,“is_priority1”:true,“is_priority2”:false},{“name”:“PID”,“count”:0,“state”:0,“is_priority1”:true,“is_priority2”:false}, {“name”:“TS”,“count”:0,“state”:0,“is_priority1”:false,“is_priority2”:true},{“name”:“CRC”,“count”:0,“ state“:0,”is_priority1“:false,”is_priority2“:true},{”name“:”PCR“,”count“:0,”state“:0,”is_priority1“:false,”is_priority2“:true }, {“名称“:”ACC“,”count“:0,”state“:0,”is_priority1“:false,”is_priority2“:true},{”name“:”PTS“,”count“:0,”state“: 0,“is_priority1”:false,“is_priority2”:true},{“name”:“CAT”,“count”:0,“state”:0,“is_priority1”:false,“is_priority2”:true}], “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}

编辑我找到了答案,我将发布

1 个答案:

答案 0 :(得分:1)

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

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