gpt4 book ai didi

python - PostgreSQL:无法删除名为 "user"的特定表

转载 作者:太空狗 更新时间:2023-10-30 01:00:02 24 4
gpt4 key购买 nike

我无法删除我的 PostgreSQL 数据库中的特定表。该表称为“用户”。当我尝试运行下面的代码片段时,

    import psycopg2
conn = psycopg2.connect("dbname='mydatabase' user='postgres' host='localhost' password='mypassword'")
cur = conn.cursor()
cur.execute("DROP TABLE user;")
conn.commit()
conn.close()

它吐出以下错误

  Traceback (most recent call last):
File "dev_psycog.py", line 20, in <module>
cur.execute("DROP TABLE user;")
psycopg2.ProgrammingError: syntax error at or near "user"
LINE 1: DROP TABLE user;

我可以很好地删除数据库中的任何其他表,但我似乎无法删除名为“user”的表。是因为“用户”是reserved keyword吗? ?

最佳答案

如下引用“用户”

import psycopg2
conn = psycopg2.connect("dbname='mydatabase' user='postgres' host='localhost' password='mypassword'")
cur = conn.cursor()
cur.execute('DROP TABLE "user";')
conn.commit()
conn.close()

参见 here .

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes (").

关于python - PostgreSQL:无法删除名为 "user"的特定表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39214369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com