#!/bin/perl -w

use strict;

use DBI;
use XML::Twig;

my $file= shift;

my $dbh= connect_to_db();
# prepare the select just once, the actual values will replace the ? later
my $insert= $dbh->prepare(  "INSERT INTO team (name, team) VALUES (?, ?);");

my $twig= new XML::Twig( twig_handlers => { name => \&insert_row});

$twig->parsefile( $file);

$dbh->disconnect();
exit;



# connect to the data base
sub connect_to_db
  { my $driver = "mysql";
    my $dsn = "DBI:$driver:database=test;";
    my $dbh = DBI->connect($dsn, 'test', '', {AutoCommit=>1});
    my $drh = DBI->install_driver($driver);
    return( $dbh);
  }


sub insert_row
  { my( $twig, $ename)= @_;
    my $name= $ename->text;
    my $team= $ename->parent->att( 'name');


    # finalize the select
    $insert->bind_param( 1, $name);          # refers to the first ? in the query
    $insert->bind_param( 2, $team);          # refers to the second ? in the query

    $insert->execute();                      # excute the SQL statement

    $twig->purge;                            # will not delete the parent

  }
    

