在 Python 中,AWS DynamoDB execute_statement 不会自动推断或转换数据类型。因此,我们需要手动设置数据类型。
以下是一个示例代码,展示如何在执行 execute_statement 命令时设置数据类型:
import boto3
# 创建 DynamoDB 客户端
dynamodb = boto3.client('dynamodb')
# 定义要执行的命令
command = """
INSERT INTO my_table (name, age, height)
VALUES (:name, :age, :height)
"""
# 定义参数及其数据类型
params = {
':name': {'S': 'John Doe'},
':age': {'N': '28'},
':height': {'N': '180'}
}
# 执行 execute_statement 命令
response = dynamodb.execute_statement(
Statement=command,
Parameters=params
)
# 输出响应结果
print(response)
在这个示例中,我们使用了 DynamoDB 类型映射对象来设置参数及其对应的数据类型。在参数名前面加上数据类型前缀,例如“S”表示字符串和“N”表示数字,即可设置参数的数据类型。
通过这种方法,我们可以在 Python 中使用 AWS DynamoDB execute_statement 命令,并且正确设置数据类型来处理数据。