Just a few thoughts see below... On Thu, 7 Aug 2003, NIPP, SCOTT V (SBCSI) wrote:
> I am working on a script to collect system information, and I have > run into a problem I cannot quite figure out. Here is the pertinent section > of code: > > my $test = $dbh->prepare("SELECT id FROM fsref WHERE server ='$server' AND > fs_ > name = '$fs_name'"); You might want to try using placeholders so: my $test = $dbh->preapre(q{ SELECT id FROM fsref WHERE server = ? AND fs_name = ? }); $test->execute($server, $fs_name); This will keep you code robust in the case where you get a "'" in $server/$fs_name -- onenever knows. > $test->execute (); > > $rows = $test->rows; $rows is not guaranteed until you have fetched() all of the data. > print "Server name: $server Filesystem: $fs_name \n"; > print "Number of matching rows: $rows \n"; > if ($rows == 1) { Do you mean ==1 ? Because if the select return more than 1 row, you will fall down to the else clause. > my $vals = $test->fetchrow_hashref ('NAME_lc'); > my $id = $vals->{id}; > my $query = $dbh->prepare("SELECT * FROM fsdata WHERE id ='$id'"); > $query->execute (); > while (my $ref = $query->fetchrow_hashref ('NAME_lc')) { > undef $set; > foreach my $key (keys %$ref) { > unless ($$key eq $ref->{$key}) { say %$ref contains the key 'keyname', then $$key is ${'keyname'} which is $keyname which would probably be undef. > $set .= " , " if $set; > $set .= $key . "=\'$$key\'"; $set .= $key . "= '".$$key == ${'keyvalue'} == undef . "."; > } > } > if ($set) { > print "Entry found in database. Updating information for $id. \n"; > my $sql = "UPDATE fsdata SET $set WHERE id = '$id'"; Are you setting the id of more than one row to 0 here? > my $sth = $dbh->prepare(qq{$sql}); > $sth->execute; > } > } > } else { > $dbh->do("INSERT INTO fsref VALUES('$id','$server','$fs_name')") or Where does $id come from? You are inserting $id which is not defined in this scope (or at least not in the section of code that you posted) Are you 'using strict'. > print "E > rror updating fsref table: ", $dbh->errstr, "\n"; > $id = $dbh->{mysql_insertid}; You are asking for a insertid; however, you told mysql what id you wanted to use when you did the insert -- Is the id column set to AUTO_INCREMENT? > $dbh->do("INSERT INTO fsdata > VALUES('$id','$lvol','$stripe_num','$stripe_siz > e','$size','$owner','$group','$export')") or print "Error updating fsdata > table: > ", $dbh->errstr, "\n"; > print "Entry not found in database. Adding information for $id. \n"; > } > } > > The code before this appears to be functioning properly, and is the > actual data collection commands i.e. bdf, vgdisplay, etc. The problem is > that I am getting some error messages that I do not understand what is > causing them. Any help would be greatly appreciated. Here is a portion of > the output errors. > > Server name: stanley Filesystem: /var > Number of matching rows: 1 > Use of uninitialized value in string eq at ./space_data.pl line 53. > Use of uninitialized value in concatenation (.) at ./space_data.pl line 55. > Entry found in database. Updating information for 23. > DBD::mysql::st execute failed: Duplicate entry '0' for key 1 at > ./space_data.pl > line 62. > > Note: The first line of the included code is line 39. Thanks. Rudy>