英文:
How to update count in dynamo table with 2 fields
问题
# Dynamodb表格有1个分区键和2个字段。我试图将a或b增加1。
# 从表格中获取a或b的cur_count(为0)并将其加1。
# 错误:
发生错误(ValidationException)
调用UpdateItem操作时:
无效的UpdateExpression:语法错误;令牌:“=”附近:“#a =#cur_count”
[![io][1]][1]
def update_count(vote):
logging.info('更新计数....')
print('更新计数....')
print('投票' + str(vote))
'''
table.update_item(
Key={'voter': 'count'},
UpdateExpression="ADD #vote :incr",
ExpressionAttributeNames={'#vote': vote},
ExpressionAttributeValues={':incr': 1}
)
'''
cur_count = 0
try:
if vote == 'b':
print('提取b的当前计数')
q = table.get_item(Key={'voter':'count'})
res = q['Item']
print(res)
cur_count = int(res['b'])
print('****** 当前计数 %d ' % cur_count)
cur_count = str(cur_count)
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD #b =#cur_count + :incr",
ExpressionAttributeNames={'#cur_count': cur_count},
ExpressionAttributeValues={':incr': 1})
print('******* b %d ' % b)
elif vote == 'a':
print('提取a的当前计数')
q = table.get_item(Key={'voter':'count'})
res = q['Item']
print(res)
cur_count = int(res['a'])
print('****** 当前计数 %d ' % cur_count)
cur_count = str(cur_count)
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD #a =#cur_count + :incr",
ExpressionAttributeNames={'#cur_count': cur_count},
ExpressionAttributeValues={':incr': 1})
print('***** a %d ' % a)
except Exception as e:
print('在这里捕获错误')
print(e)
英文:
The Dynamodb table has 1 partition key and 2 fields. I'm trying to increment a or b by 1.
I get cur_count for a or b(which is 0) from the table and +1 to it.
The error:
An error occurred (ValidationException)
When calling the UpdateItem operation:
Invalid UpdateExpression: Syntax error; token: "=", near: "#a =#cur_count"
def update_count(vote):
logging.info('update count....')
print('update count....')
print('vote' + str(vote))
'''
table.update_item(
Key={'voter': 'count'},
UpdateExpression="ADD #vote :incr",
ExpressionAttributeNames={'#vote': vote},
ExpressionAttributeValues={':incr': 1}
)
'''
cur_count = 0
try:
if vote == 'b':
print('extracting cur count for b')
q = table.get_item(Key={'voter':'count'})
res = q['Item']
print(res)
cur_count = int(res['b'])
print('****** cur count %d ' % cur_count)
cur_count = str(cur_count)
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD #b =#cur_count + :incr",
ExpressionAttributeNames={'#cur_count': cur_count},
ExpressionAttributeValues={':incr': 1})
print('******* b %d ' % b)
elif vote == 'a':
print('extracting cur count for a')
q = table.get_item(Key={'voter':'count'})
res = q['Item']
print(res)
cur_count = int(res['a'])
print('****** cur count %d ' % cur_count)
cur_count = str(cur_count)
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD #a =#cur_count + :incr",
ExpressionAttributeNames={'#cur_count': cur_count},
ExpressionAttributeValues={':incr': 1})
print('***** a %d ' % a)
except Exception as e:
print('catching error here')
print(e)
答案1
得分: 1
你定义了 #a
或 #b
作为列名表达式,但没有为这些列进行映射。
有两种修复此问题的选项。
table.update_item(
Key={'voter': 'count'},
UpdateExpression="ADD #column :incr",
ExpressionAttributeNames={'#column': 'b'},
ExpressionAttributeValues={':incr': 1}
)
或
table.update_item(
Key={'voter': 'count'},
UpdateExpression="ADD b :incr",
ExpressionAttributeValues={':incr': 1}
)
英文:
You define #a
or #b
as column name expressions but there is no mapping for this columns.
There are two options to fix this issue.
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD #column :incr",
ExpressionAttributeNames={'#column': 'b'},
ExpressionAttributeValues={':incr': 1}
)
or
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD b :incr",
ExpressionAttributeValues={':incr': 1}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论