ConditionExpression attribute_not_exists()
P粉132730839
P粉132730839 2024-03-29 09:20:08
0
1
370

我尝试在数据库中插入项目时使用 ConditionExpression,但它不起作用,当 Putitem() 函数运行时,php 脚本会中断。

如果该项目不存在,我想插入该项目。

$response = $client->putItem(array(
    'TableName' => 'tablename',
    'Item' => array(
        'serialNumber'   => array('S' => 'test123'),
        'deviceType' => array('S' => '1')
    ),
    'ConditionExpression' => 'attribute_not_exists(serialNumber)'
));

我尝试 var_dump $response,但代码在上面的函数上中断。

serialNumber 它是一个分区键,应该按预期工作。

下面的代码工作正常,但他用新值替换现有项目,这是我不希望发生的情况。

$response = $client->putItem(array(
    'TableName' => 'tablename',
    'Item' => array(
        'serialNumber'   => array('S' => 'test123'),
        'deviceType' => array('S' => '1')
    )
));

P粉132730839
P粉132730839

全部回复(1)
P粉667649253

当您设置的条件计算结果为 false 时,预计您会返回 CondidtionCheckFailedException。尝试将代码包装在 try/catch 块中,看看它是否按预期工作?

try {
$response = $client->putItem(array(
    'TableName' => 'tablename',
    'Item' => array(
        'serialNumber'   => array('S' => 'test123'),
        'deviceType' => array('S' => '1')
    )
));
}
catch(Exception $e) {
  echo $e->getMessage();
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!