MySQL Forums
Forum List  »  Perl

AutoCommit issue
Posted by: chen gene
Date: February 05, 2015 08:12PM

Hi, I set autocommit=1 in my perl script, but found the record cannot be inserted.
[root@dhcpc tmp]# cat dbd1.pl
#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", "root", "123456", { 'RaiseError' => 1, AutoCommit => 1 } );
my $sql =qq{insert into test(name, id) values('hello ', 2)};
$dbh->do($sql);

my $query = $dbh->prepare("SELECT name FROM test");
$query->execute();

while ( $ary = $query->fetchrow_arrayref() ) {
print ( $ary->[0] . "\n");
}

[root@dhcpc tmp]# perl dbd1.pl
hello
[root@dhcpc tmp]# perl dbd1.pl
hello

but if I execute commit, it runs as expected.
[root@dhcpc tmp]# cat dbd2.pl
#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", "root", "123456", {AutoCommit => 0 } ) ;
# my $dbh = DBI->connect( "DBI:mysql:database=test;host=localhost", "root", "123456", { 'RaiseError' => 1, AutoCommit => 1 } );
my $sql =qq{insert into test(name, id) values('hello ', 2)};
$dbh->do($sql);

my $query = $dbh->prepare("SELECT name FROM test");
$query->execute();
$dbh->commit();

while ( $ary = $query->fetchrow_arrayref() ) {
print ( $ary->[0] . "\n");
}

[root@dhcpc tmp]# perl dbd2.pl
hello
[root@dhcpc tmp]# perl dbd2.pl
hello
hello

btw, mysql is 5.6.22 and I set autocommit=off in /etc/my.cnf

Options: ReplyQuote


Subject
Written By
Posted
AutoCommit issue
February 05, 2015 08:12PM
February 07, 2015 08:34PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.