Spenser has asked for the wisdom of the Perl Monks concerning the following question:
I have a script which basically (among other things) I want to read the contents of some XML files into a table in MySQL. I'm using DBI.pm to interact with MySQL. Everything else is working fine: I'm able to read the files, parse them a bit for other things, but at the end of the script, when I try to reopen the files to copy their total contents to MySQL, the contents aren't saved. Below is the sub-routine which I wrote to handle this part:
sub xmlfiles_to_mysql { my $xmlfile_contents; while(my ($file_id,$xml_file) = each(%xml_files) ) { open(XML_FILE, "./xml_files/$xml_file"); while (<XML_FILE>) { chomp; $xmlfile_contents .= $_; } my $sql_stmnt = "REPLACE INTO xml_files (file_id, xml_file) VALUES(?,?)"; my $sth = $dbh->prepare($sql_stmnt); $sth->execute($file_id,$xmlfile_contents); $sth->finish(); close XML_FILE; } }
The hash %xml_files is set up outside the sub-routine and seems to work alright. The table in MySQL have the names of the xml filse in the first column. They have NULL for the xml_file column. That column is a BLOB column.
I'm getting this error message for each file read:
readline() on closed filehandle XML_FILE at xml_processing.pl line 275.Obviously the file is closing before it's finished. But I don't see how. Any thoughts on what I'm doing wrong or in the wrong order? I know it's probably simple, but I just don't see it. Thanks in advance for any help.
-Spenser
That's Spenser, with an "s" like the detective.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Copying Files to MySQL
by Fletch (Bishop) on Apr 20, 2007 at 17:42 UTC | |
by johngg (Canon) on Apr 20, 2007 at 20:30 UTC | |
|
Re: Copying Files to MySQL
by wjw (Priest) on Apr 20, 2007 at 20:37 UTC | |
|
Re: Copying Files to MySQL
by Cody Pendant (Prior) on Apr 21, 2007 at 13:05 UTC |