psycopg2.errors.SyntaxError: syntax error at or near
psycopg2.errors.SyntaxError: syntax error at or near
每当我尝试更新一行时,Psycopg2 都会给我这个错误:
Traceback (most recent call last):
File "p:/Projects/Freelance/Optometry_Inventory/code/tester.py", line 39, in <module>
new_user.updateUser(*(fname, lname, username, role, hashed_pwd, user_id))
File "p:\Projects\Freelance\Optometry_Inventory\code\UserDC.py", line 64, in updateUser
cursor.execute(update_query, args)
psycopg2.errors.SyntaxError: syntax error at or near "lname"
LINE 1: UPDATE users SET fname= ('a'), SET lname= ('b'),
当我运行以下函数时:
def updateUser(self, user_id):
self.user_id = user_id
update_query = """UPDATE users SET fname= %s, SET lname= (%s),
SET username= (%s), SET user_role= (%s), SET h_pwd= (%s), WHERE user_id= (%s)"""
with pool() as cursor:
cursor.execute(update_query, (self.fname, self.lname, self.user_name,
self.user_role, self.user_pwd, self.user_id))
我试过只更新一个字段,但我仍然遇到同样的错误。还有什么我想念的吗?错误代码没有提供太多信息。
问题是您为每个修改的列冗余地写了 SET
,一个 SET
就足够了:)
def updateUser(self, user_id):
self.user_id = user_id
update_query = """
UPDATE users
SET fname= %s,
lname= (%s),
username= (%s),
user_role= (%s),
h_pwd= (%s)
WHERE user_id= (%s)"""
with pool() as cursor:
cursor.execute(update_query, (self.fname, self.lname, self.user_name,
self.user_role, self.user_pwd, self.user_id))
如果您想节省一些打字时间,还有:
import psycopg2
con = psycopg2.connect(...)
cur = con.cursor()
update_query = """
UPDATE users
SET
(fname, lname, username,user_role, h_pwd)
=
(%s, %s, %s, %s, %s)
WHERE user_id= (%s)"""
cursor.execute(update_query, (self.fname, self.lname, self.user_name,
self.user_role, self.user_pwd, self.user_id))
如果您一次进行多个更新,您可能还想看看 execute_values。在 link 的 execute_values 部分有一个 UPDATE
示例。
每当我尝试更新一行时,Psycopg2 都会给我这个错误:
Traceback (most recent call last):
File "p:/Projects/Freelance/Optometry_Inventory/code/tester.py", line 39, in <module>
new_user.updateUser(*(fname, lname, username, role, hashed_pwd, user_id))
File "p:\Projects\Freelance\Optometry_Inventory\code\UserDC.py", line 64, in updateUser
cursor.execute(update_query, args)
psycopg2.errors.SyntaxError: syntax error at or near "lname"
LINE 1: UPDATE users SET fname= ('a'), SET lname= ('b'),
当我运行以下函数时:
def updateUser(self, user_id):
self.user_id = user_id
update_query = """UPDATE users SET fname= %s, SET lname= (%s),
SET username= (%s), SET user_role= (%s), SET h_pwd= (%s), WHERE user_id= (%s)"""
with pool() as cursor:
cursor.execute(update_query, (self.fname, self.lname, self.user_name,
self.user_role, self.user_pwd, self.user_id))
我试过只更新一个字段,但我仍然遇到同样的错误。还有什么我想念的吗?错误代码没有提供太多信息。
问题是您为每个修改的列冗余地写了 SET
,一个 SET
就足够了:)
def updateUser(self, user_id):
self.user_id = user_id
update_query = """
UPDATE users
SET fname= %s,
lname= (%s),
username= (%s),
user_role= (%s),
h_pwd= (%s)
WHERE user_id= (%s)"""
with pool() as cursor:
cursor.execute(update_query, (self.fname, self.lname, self.user_name,
self.user_role, self.user_pwd, self.user_id))
如果您想节省一些打字时间,还有:
import psycopg2
con = psycopg2.connect(...)
cur = con.cursor()
update_query = """
UPDATE users
SET
(fname, lname, username,user_role, h_pwd)
=
(%s, %s, %s, %s, %s)
WHERE user_id= (%s)"""
cursor.execute(update_query, (self.fname, self.lname, self.user_name,
self.user_role, self.user_pwd, self.user_id))
如果您一次进行多个更新,您可能还想看看 execute_values。在 link 的 execute_values 部分有一个 UPDATE
示例。