gpt4 book ai didi

mysql - 尝试....在 mysql 中捕获事务?

转载 作者:IT王子 更新时间:2023-10-29 00:34:56 27 4
gpt4 key购买 nike

当SQL语句出现错误系统会自动回滚更改时,如何启动事务?

Transaction MySQL

PHP + MySQL transactions examples

在 PHP 中

try {
// First of all, let's begin a transaction
$db->beginTransaction();

// A set of queries; if one fails, an exception should be thrown
$db->query('first query');
$db->query('second query');
$db->query('third query');

// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}

如何在没有PHP,只有MYSQL的情况下重复逻辑

最佳答案

我们可以将多个查询写入MySQL 过程/函数,并可以维护事务。请引用下面给出的示例。基本上,您应该声明一个将调用回滚的错误处理程序

PROCEDURE `myprocedure`()
BEGIN

.. Declare statements ..

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
.. set any flags etc eg. SET @flag = 0; ..
ROLLBACK;
END;

START TRANSACTION;

.. Query 1 ..
.. Query 2 ..
.. Query 3 ..

COMMIT;
.. eg. SET @flag = 1; ..

END

详情请见下方链接

MySQL : transaction within a stored procedure

How can I use transactions in my MySQL stored procedure?

关于mysql - 尝试....在 mysql 中捕获事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30973002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com