【问题标题】:Psycopg2 issue inserting values into an existing table in the databasePsycopg2 问题将值插入到数据库中的现有表中
【发布时间】:2016-11-23 16:03:11
【问题描述】:

我很难理解为什么 psycopg2 对“用户”这个词有问题。我正在尝试将值插入一个名为 user 的表中,其中包含 user_id、name、password 列。我在“用户”处或附近收到编程错误:语法错误。 open_cursor() 是一个用于打开游标进行数据库操作的函数。

这是我的代码:

query = """INSERT INTO user (name, password) VALUES (%s, %s);"""
data = ('psycouser', 'sha1$ba316b$52dd71da1e331247f0a7ab869e1b072210add9c1')
with open_cursor() as cursor:
    cursor.execute(query, data)
    print "Done."

【问题讨论】:

  • 请始终引用表名。

标签: python postgresql psycopg2


【解决方案1】:

因为 user 是 sql 语言的一部分。

尝试使用 dbl 引号:

query = 'INSERT INTO "user" (name, password) VALUES (%s, %s);'

【讨论】:

    猜你喜欢
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2013-08-27
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多