条件expression attribute_not_exists()

发布于 2025-02-06 20:23:16 字数 746 浏览 0 评论 0原文

在数据库上插入项目时,我正在尝试使用条件表达,但是在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 var_dump上面的函数折断。

SerialNumber它的分区密钥,应该按预期工作。

下面的代码正常运行,但他用新值代替了现有项目,这是我不想发生的。

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

I am trying to use ConditionExpression when inserting an item on the database, but it dont work, the php script breaks when the Putitem() function runs.

I want to insert the item if he dont exist.

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

I tried to var_dump the $response but the code breaks on the function above.

serialNumber its a Partition Key which should work as intended.

The code below works fine, but he replaces the existing item with new values, which its what i dont want to happen.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-02-13 20:23:16

预计您将返回condidtioncheckfailedexception当您设置为false的条件时。尝试将代码包装在尝试/捕获块中以查看它是否可以按预期工作?

try {
$response = $client->putItem(array(
    'TableName' => 'tablename',
    'Item' => array(
        'serialNumber'   => array('S' => 'test123'),
        'deviceType' => array('S' => '1')
    )
));
}
catch(Exception $e) {
  echo $e->getMessage();
}

It is expected that you are returned a CondidtionCheckFailedException when the condition you set evaluates to false. Try wrapping your code in a try/catch block to see if it works as expected?

try {
$response = $client->putItem(array(
    'TableName' => 'tablename',
    'Item' => array(
        'serialNumber'   => array('S' => 'test123'),
        'deviceType' => array('S' => '1')
    )
));
}
catch(Exception $e) {
  echo $e->getMessage();
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文