psycopg2 执行许多语句失败并出现语法错误

发布于 2024-12-15 05:55:23 字数 709 浏览 3 评论 0原文

我有来自 mongodb 的数据,

data = 
(
 {
   u'name': 'A',
   u'primary_key': 1
 },
 {
   u'name': 'B',
   u'primary_key': 2
 },
 {
   u'name': 'C',
   u'primary_key': 3
 }
)

当我调用以下内容时,

cur = conn.cursor()
cur.executemany("""INSERT INTO ddmension(id,name) VALUES (%(primary_key)s, %(name)s)""", data) 

它看起来失败说

ProgrammingError: 'syntax error at or near """"INSERT INTO dimension (id, name) VALUES (1, E\'A\')""""\nLINE 1: """INSERT INTO dimension (id, name) VALUES (1, E\'A\n ^\n'  

有人可以指导我问题可能是什么吗?我正在关注这个教程

谢谢

I have data coming from mongodb which looks like

data = 
(
 {
   u'name': 'A',
   u'primary_key': 1
 },
 {
   u'name': 'B',
   u'primary_key': 2
 },
 {
   u'name': 'C',
   u'primary_key': 3
 }
)

when I call the following

cur = conn.cursor()
cur.executemany("""INSERT INTO ddmension(id,name) VALUES (%(primary_key)s, %(name)s)""", data) 

it fails saying

ProgrammingError: 'syntax error at or near """"INSERT INTO dimension (id, name) VALUES (1, E\'A\')""""\nLINE 1: """INSERT INTO dimension (id, name) VALUES (1, E\'A\n ^\n'  

Can someone guide me what the issue could be? I am following this tutorial

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-12-22 05:55:23

我们应该尝试取消引用该错误消息,以便更清楚地了解这里发生的情况。我们可以在 Python 提示符下执行此操作:

>>> message = 'syntax error at or near """"INSERT INTO dimension (id, name) VALUES (1, E\'A\')""""\nLINE 1: """INSERT INTO dimension (id, name) VALUES (1, E\'A\n ^\n'
>>> print message
syntax error at or near """"INSERT INTO dimension (id, name) VALUES (1, E'A')""""
LINE 1: """INSERT INTO dimension (id, name) VALUES (1, E'A
 ^

啊,好多了。

通常,Postgres 会小心地使用小 ^ 字符来指向它混淆的行中的确切位置 - 但在这里,它指向它所混淆的“Line 1”标签的中间。放在混淆它的行的前面。可能您在 Stack Overflow 中的剪切和粘贴操作将几个连续的空格折叠在一起,如果您的编辑器或浏览器心情不好,则可能会发生这种情况。

所以我无法准确判断错误发生在该行的哪个位置,但我有一个非常强烈的猜测:不知何故,你试图用它包围你的语句的Python三引号实际上被传递到了Postgres!在 Postgres 提示符下玩了几分钟后,我找到的获取“LINE”以三引号开头的“语法错误”消息的唯一方法就是手动将三引号键入到SQL,它们不属于其中(因为 Postgres 不理解三引号;这是 Python 约定):

$ psql postgres 
Null display is "NULL".
psql (8.4.8)
Type "help" for help.

postgres=# """SELECT 1""";
ERROR:  syntax error at or near """"SELECT 1""""
LINE 1: """SELECT 1""";
        ^

问题是,对于您所显示的示例代码来说,此错误是不可能的。要得到这个错误,你实际上必须输入这样的 Python 代码:

cur.executemany('"""INSERT INTO ddmension(id,name) ..."""', data) 

还有其他方法可以在 Python 字符串中获取三引号,但这是最简单的。无论如何,您的 Postgres 错误消息肯定表明三重引号进入了您的 SQL,因此请尝试再次检查您的 Python 代码,我们将帮助您弄清楚文字引号是如何进入您的字符串的!

We should try un-quoting that error message to see a bit more clearly what is going on here. We can do that at the Python prompt:

>>> message = 'syntax error at or near """"INSERT INTO dimension (id, name) VALUES (1, E\'A\')""""\nLINE 1: """INSERT INTO dimension (id, name) VALUES (1, E\'A\n ^\n'
>>> print message
syntax error at or near """"INSERT INTO dimension (id, name) VALUES (1, E'A')""""
LINE 1: """INSERT INTO dimension (id, name) VALUES (1, E'A
 ^

Ahh, much better.

Normally, Postgres is careful to use the little ^ character to point at the exact place in the line where it got confused — but here, it is pointing at the middle of the “Line 1” label that it puts in front of the line that confused it. Probably your cut-and-paste into Stack Overflow collapsed several consecutive spaces together, which can happen if your editor or browser were in a disagreeable mood.

So I cannot tell exactly where in the line the error occurred, but I have a very strong guess: somehow, the Python triple-quotes with which you are trying to surround your statement are actually getting passed in to Postgres! After several minutes of playing around at the Postgres prompt, the only way I can find to get a “syntax error” message whose ”LINE” starts with triple quotes is to actually type the triple quotes manually into the SQL, where they do not belong (since Postgres does not understand triple-quoting; it's a Python convention):

$ psql postgres 
Null display is "NULL".
psql (8.4.8)
Type "help" for help.

postgres=# """SELECT 1""";
ERROR:  syntax error at or near """"SELECT 1""""
LINE 1: """SELECT 1""";
        ^

The problem is that this error is impossible with the sample code that you have displayed. To get this error, you would have had to in fact type Python code like this:

cur.executemany('"""INSERT INTO ddmension(id,name) ..."""', data) 

There are other ways to get triple-quotes inside of a Python string, but this is the easiest. In any case, your Postgres error message is most certainly showing that triple quotes are getting inside of your SQL, so try checking your Python code again, and we will help you figure out how literal quote marks are winding up inside your strings!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文