{"id":84,"date":"2014-09-11T20:44:37","date_gmt":"2014-09-11T20:44:37","guid":{"rendered":"http:\/\/baselines.com\/blog\/?p=84"},"modified":"2014-09-11T20:57:16","modified_gmt":"2014-09-11T20:57:16","slug":"84","status":"publish","type":"post","link":"https:\/\/baselines.com\/blog\/?p=84","title":{"rendered":"Perl MySQL Transaction example"},"content":{"rendered":"<p><a href=\"http:\/\/baselines.com\/blog\/wp-content\/uploads\/2014\/09\/transactions.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-88\" src=\"http:\/\/baselines.com\/blog\/wp-content\/uploads\/2014\/09\/transactions-228x300.gif\" alt=\"transactions\" width=\"228\" height=\"300\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>use strict;<br \/>\nuse warnings;<br \/>\nuse v5.10; # for say() function<\/p>\n<p>use DBI;<\/p>\n<p>say &#8220;Perl MySQL Transaction Demo&#8221;;<\/p>\n<p># MySQL database configurations<br \/>\nmy $dsn = &#8220;DBI:mysql:perlmysqldb&#8221;;<br \/>\nmy $username = &#8220;root&#8221;;<br \/>\nmy $password = &#8221;;<\/p>\n<p># connect to MySQL database<br \/>\nmy %attr = (RaiseError=&amp;gt;1,\u00a0 # error handling enabled<br \/>\nAutoCommit=&amp;gt;0); # transaction enabled<\/p>\n<p>my $dbh = DBI-&amp;gt;connect($dsn,$username,$password, \\%attr);<\/p>\n<p>eval{<br \/>\n# insert a new link<br \/>\nmy $sql = &#8220;INSERT INTO links(title,url,target)<br \/>\nVALUES(?,?,?)&#8221;;<br \/>\nmy $sth = $dbh-&amp;gt;prepare($sql);<br \/>\n$sth-&amp;gt;execute(&#8220;Comprehensive Perl Archive Network&#8221;,&#8221;http:\/\/www.cpan.org\/&#8221;,&#8221;_blank&#8221;);<br \/>\n# get last insert id of the link<br \/>\nmy $link_id = $dbh-&amp;gt;{q{mysql_insertid}};<\/p>\n<p># insert a new tag<br \/>\n$sql = &#8220;INSERT INTO tags(tag) VALUES(?)&#8221;;<br \/>\n$sth = $dbh-&amp;gt;prepare($sql);<br \/>\n$sth-&amp;gt;execute(&#8216;Perl&#8217;);<\/p>\n<p># get last insert id of the tag<br \/>\nmy $tag_id = $dbh-&amp;gt;{q{mysql_insertid}};<\/p>\n<p># insert a new link and tag relationship<br \/>\n$sql = &#8220;INSERT INTO link_tags(link_id,tag_id)<br \/>\nVALUES(?,?)&#8221;;<br \/>\n$sth = $dbh-&amp;gt;prepare($sql);<br \/>\n$sth-&amp;gt;execute($link_id,$tag_id);<\/p>\n<p># if everything is OK, commit to the database<br \/>\n$dbh-&amp;gt;commit();<br \/>\nsay &#8220;Link and tag has been inserted and associated successfully!&#8221;;<br \/>\n};<\/p>\n<p>if($@){<br \/>\nsay &#8220;Error inserting the link and tag: $@&#8221;;<br \/>\n$dbh-&amp;gt;rollback();<br \/>\n}<\/p>\n<p># disconnect from the MySQL database<br \/>\n$dbh-&amp;gt;disconnect();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; use strict; use warnings; use v5.10; # for say() function use DBI; say &#8220;Perl MySQL Transaction Demo&#8221;; # MySQL database configurations my $dsn = &#8220;DBI:mysql:perlmysqldb&#8221;; my $username = &#8220;root&#8221;; my $password = &#8221;; # connect to MySQL database my %attr = (RaiseError=&amp;gt;1,\u00a0 # error handling enabled AutoCommit=&amp;gt;0); # transaction enabled my $dbh = DBI-&amp;gt;connect($dsn,$username,$password, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84"}],"version-history":[{"count":6,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baselines.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}