python-3.x 用于向列表属性追加值的PartiQL INSERT语句

t5fffqht  于 2023-01-14  发布在  Python
关注(0)|答案(1)|浏览(109)

我正在尝试使用PartiQL语法向DynamoDB中的现有列表属性追加值。
项目的主键包括:

pk = userId
sk = happeningId
table_name = test_table
userId = "user09hfh47egd53tgd"
happeningId = "happ09hdg2536dget7354tdg"
contactId = "C0003"
decision = "accept"
if decision == "accept":

    stmt = f"UPDATE \"{table_name}\" SET accept = list_append(accept, :'{contactId}') WHERE pk='{userId}' AND sk='{happeningId}'"
    print(stmt)
    resp = dynamodb_client.execute_statement( Statement=stmt )
else:
    stmt = f"UPDATE \"{table_name}\" SET cancel = list_append(cancel, :'{contactId}') WHERE pk='{userId}' AND sk='{happeningId}'"
    print(stmt)
    resp = dynamodb_client.execute_statement( Statement=stmt )

最后一个字符串如下所示

UPDATE "test_table" SET accept = list_append(accept, :'C0003') WHERE pk='user09hfh47egd53tgd' AND sk='happ09hdg2536dget7354tdg'

当我尝试运行我的代码时,我收到以下错误消息:

ClientError: An error occurred (ValidationException) when calling the ExecuteStatement operation: Statement wasn't well formed, can't be processed: Unexpected term

有人知道我做错了什么吗?

vngu2lb8

vngu2lb81#

试着这样写你的陈述:
联系人ID =["C0003"]

stmt = f"UPDATE \"{table_name}\" SET accept = list_append(accept, {contactId}) WHERE pk='{userId}' AND sk='{happeningId}'"

说明:您想要附加到列表的值也需要列表类型。此外,双点不是必需的,并且需要删除'{contactId}'中的引号,因为它们会将插入的值转换为字符串。正如我所说,值也需要列表类型。
希望能有所帮助。

相关问题