use strict; use warnings; use utf8; use diagnostics; use DBI; use xml::twig; our %mirror; our %referential; our $table; our $rows; our $columns; our $dbh= connect_to_db() || die "Can't connect: $DBI::errstr\n"; my $file ="poorlySTRUCTURED.xml"; my $twig=XML::Twig->new( twig_handlers=> {lots of handlers that call the elements I need}); $twig->parsefile("$file"); sub connect_to_db { my $driver = "mysql"; my $drh = DBI->install_driver($driver); my $dsn = "DBI:$driver:database=united;host=somehost;port=anyportinastorm"; my $dbh = DBI->connect($dsn, "root", "password", {AutoCommit=>1}; $dbh->{RaiseError}=1;); return( $dbh); } sub insertSKU{ for my $Itemnumber (keys %mirror){ for $table (keys %{$mirror{$Itemnumber}}){ if ($table=~/skugroup/){ $columns= (join ', ', keys %{$mirror{"$Itemnumber"}{"$table"}}); $rows= (join ', ', values %{$mirror{"$Itemnumber"}{"$table"}}); #Now we sanitize the data for MySQL syntax. $columns =~s/\.|;|\/|\\|\|//g; $rows =~s/\.|;|\/|\\|\|//g; my $insert= $dbh->prepare("INSERT INTO $table ($columns) VALUES ($rows)GO"); $insert->execute() || die "Insert failed \n $columns \n\n $rows"; } } } }