I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2008-04-04 03:42:54-07 by nmatchan
InnoDB problem - INSERT works, SELECT gets 0 rows
Code levels: Redhat EL3, perl-DBI-1.602-1.el3.rf, perl-DBD-mysql-4.006-1.el3.rf, perl-PlRPC-0.2020-1.el3.rf, perl-5.8.0-97.EL3, MySQL-devel-community-5.1.23-0.rhel3, MySQL-shared-compat-5.1.23-0.rhel3, MySQL-client-community-5.1.23-0.rhel3 and MySQL-server-community-5.1.23-0.rhel3.
I have defined a table to use the InnoDB engine so that I can use the FOREIGN KEY feature. If I manually execute the SQL "INSERT" command line from the MySQL command line client, the INSERT is successful and a SELECT * FROM [table_name]; returns the row data ok. If I DROP and CREATE the table (to clear it out) and execute the same INSERT command from a perl script, $DBI::errstr is not set (no error) but SELECT * from [table_name]; returns 0 rows. Re-running the script should produce a 'duplicate key' error, but it doesn't. If I execute two identical INSERT commands in the same script one after the other, then I get a 'duplicate key' error on the second INSERT executed but 'SELECT * FROM [table_name];' still returns 0 rows. Re-running the double INSERT script still only produces the one 'duplicate' error on the second INSERT, not the first INSERT. As this works ok from the MySQL-client-community interface, I believe it's either a perl DBD or DBI problem. I have updated both perl DBD/DBI & MySQL to the latest versions I can find and the problem is still occuring. If I redefine the TABLE to use the MyISAM engine, the same INSERT script works ok (SELECT * from .... returns rows of data ok) but this engine doesn't support referential integrity via foreign keys. Any suggestions please? Test code bits and pieces:
CREATE TABLE network_names (network_name_key INT UNSIGNED NOT NULL AUTO_INCREMENT, networkName VARC +HAR(20) not null, PRIMARY KEY (network_name_key)) ENGINE = InnoDB; INSERT INTO network_names (networkName) values('ME'); INSERT INTO network_names (networkName) values('YOU'); INSERT INTO network_names (networkName) values('PRIVATE'); CREATE TABLE router_namesx (network_name_key INT UNSIGNED NOT NULL, routerName VARCHAR(30), vlanNum +ber INT(6) UNSIGNED NOT NULL, networkSubnet INT UNSIGNED NOT NULL, index (network_name_key), PRIMA +RY KEY (routerName,vlanNumber), FOREIGN KEY (network_name_key) REFERENCES network_names (network_n +ame_key)) ENGINE = InnoDB; #! /usr/bin/perl -w # -*- mode: Perl -*- require 5.005; use strict; use warnings; use IO::Socket; use Net::hostent; use DBI; use Net::SNMP; my $db="testDB"; my $passwd="mypassword"; my $userid = "myuserid"; my $host = "localhost"; my $driver = "mysql"; my $connectionInfo="dbi:$driver:$db;$host"; my $result = read_vlanTable(); exit; sub read_vlanTable { # my $networkName = &trim(shift(@argList)); # my $host = &trim(shift(@argList)); # my $version = &trim(shift(@argList)); # my $community = &trim(shift(@argList)); # my $oid = ".1.3.6.1.4.1.9.5.1.9.2.1.1"; # VLAN table from CISCO-STACK-MIB my $networkSubnet = "0"; my $networkNameKey; my $vlanNumber; my $dbh; my $sth; my $sql; if ($dbh = DBI->connect($connectionInfo,$userid,$passwd,{RaiseError => 0,AutoCommit => 0})) { $sth = $dbh->prepare("INSERT INTO router_namesx (network_name_key,routerName,vlanNumber,network +Subnet) VALUES (?,?,?,?)"); $sth->execute("1", "EastCoast15", "10"); print $client "\r\nsql error: $DBI::errstr"; $sth->finish(); $dbh->disconnect; } return; } This works from the "mysql" command line prompt with an engine type of InnoDB on the table: INSERT INTO router_namesx (network_name_key, routerName, vlanNumber) values('1','EastCoast15','10') +;
Thanks in advance, Neil M
Direct Responses: 7584 | Write a response
Posted on 2008-04-04 07:11:33-07 by clinton in response to 7583
Re: InnoDB problem - INSERT works, SELECT gets 0 rows
Giving your post a quick skim, I'd say the problem is that you're not committing your changes. You're connecting with
AutoCommit => 0
, but you're never calling
$sth->commit
. Clint
Direct Responses: 7601 | Write a response
Posted on 2008-04-07 01:33:07-07 by nmatchan in response to 7584
Re: InnoDB problem - INSERT works, SELECT gets 0 rows
Clint, Many many thanks. I've change AutoCommit=>0 to AutoCommit=>1 and I now seeing the record data in the InnoDB table. I tried inserting $sth->commit after the $sth->execute but perl can't locate the 'commit' method in the DBI::st package. I think this is just a syntax problem so it should be to diffcult to chase up. Once again, many thanks for your assistance. Regards, Neil M
Direct Responses: Write a response