PurePerl MySQL Transaction
Using the PurePerl MySQL API– Part 10
Foreword: In this part of the series, I explain how to code a transaction in MySQL.
By: Chrysanthus Date Published: 31 Aug 2016
Introduction
START TRANSACTION and COMMIT
In the following transaction code, a value is read from the pet table and placed in another row of the pet table.
START TRANSACTION;
SELECT @species := species FROM pet WHERE owner='John';
UPDATE pet SET species=@species WHERE owner='Diane';
COMMIT;
The transaction begins with the statement “START TRANSACTION;” and ends with the statement “COMMIT;”. In between, the SQL statement results are saved temporarily and not permanently. They are saved to disk permanently with the “COMMIT;” statement.
There are some statements that are always committed whether or not they are in a transaction. Typical of these are the Data Definition Language (DDL) statements that define or modify database objects. These are: ALTER DATABASE ... UPGRADE DATA DIRECTORY NAME, ALTER EVENT, ALTER PROCEDURE, ALTER SERVER, ALTER TABLE, ALTER VIEW, CREATE DATABASE, CREATE EVENT, CREATE INDEX, CREATE PROCEDURE, CREATE SERVER, CREATE TABLE, CREATE TRIGGER, CREATE VIEW, DROP DATABASE, DROP EVENT, DROP INDEX, DROP PROCEDURE, DROP SERVER, DROP TABLE, DROP TRIGGER, DROP VIEW, RENAME TABLE, TRUNCATE TABLE.
The above transaction can be coded as follows (after selecting the database):
my $transSt = "START TRANSACTION;
SELECT \@species := species FROM pet WHERE owner='John';
UPDATE pet SET species=\@species WHERE owner='Diane';
COMMIT;";
if (Mysql::query($transSt) != 1)
{
print $Mysql::Error_msg, "\n";
}
else
{
for (my $i=0; $i<@Mysql::Result; ++$i)
{
for (my $j=0; $j<@{$Mysql::Result[$i]}; ++$j)
{
print %{${$Mysql::Result[$i]}[$j]}, "\n";
}
}
}
Thats is it for this part of the series. We stop here and continue in the next part.
Chrys
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT