in reply to Saving data to XML file using DBD::AnyData

A working example, for select and insert from/into in memory table 'users'. Put the XML from the DATA section in eadata.xml.
# always: use strict; use warnings; use DBI; my $table = 'users'; my $format = 'XML'; my $file = 'eadata.xml'; my $dbh = DBI->connect('dbi:AnyData(RaiseError => 1):'); # insert a new record in to the memory table 'users' $dbh->func( $table, $format, $file, 'ad_import'); # Select data from users my $sql1 = qq{ SELECT * FROM $table }; my $sth = $dbh->prepare($sql1); $sth->execute(); while (my $row = $sth->fetchrow_hashref){ print "Row:\n"; while (my ($key, $value) = each (%{$row})) { print " $key: $value\n"; } } # Insert a new record in to the memory table 'users' my $sql2 = qq{ INSERT INTO users (name, agency, email, org, mailstop, phone, title +) VALUES (?, ?, ?, ?, ?, ?, ?) }; $sth = $dbh->prepare($sql2); # It's easy if we have the data in a hash my $users = { name => 'Testing', agency => 'TT', email => 'test@abc.com', org => 'ABC', mailstop => 'X159', phone => '(555) 123-4567', title => 'Other', }; # Transform hash into two arrays my @fields = keys %{$users}; my @values = map { $users->{$_} } @fields; # And use a generic insert statement # code stolen from the IBPerl module examples ;) $sql2 = "INSERT INTO $table (" . join( ',', @fields ) . ') VALUES (' . ( '?,' x $#fields ) . "?)"; # print "sql=", $sql2, "\n"; $sth->execute(@values); print "\nLet's see the results:\n"; $sth = $dbh->prepare($sql1); $sth->execute(); while (my $row = $sth->fetchrow_hashref){ print "Row:\n"; while (my ($key, $value) = each (%{$row})) { print " $key: $value\n"; } }

Replies are listed 'Best First'.
Re^2: Saving data to XML file using DBD::AnyData
by warsting (Novice) on Dec 04, 2009 at 17:33 UTC

    In the "actual" web program that this snippet came from uses the strict and warning, but I left it out do to "laziness" and I knew it wasn't the problem here.

    The data for the new record would be gotten from a web form, I provided for ease of testing only. In my version I can insert the record, but the creation of the XML file is what I need help with. This is my problem, not the SQL part.

    Looking at the code you provided (and testing it), it doesn't save the "newly" inserted record to the XML file. How is this done using AnyData modules? What am I doing wrong?

      In my code there is no ad_export statement and if I add one I get the same error messages. I see now that I didn't understand what was the real problem, sorry, in this case, probably snoopy was right about what is happening.

      Anyhow the DBD-AnyData module has 8 active bugs and the last is 2 years old, I'm not sure it's ready for production.

      Best regards, Stefan

        Seeing that I couldn't get the export to work and checking when the author was going to fix the bug based on snoopy's bug report to the author which will not be until version 2.0 (whenever that will be), I decided to use XML-Simple to create the XML based on the selection on the memory table. I will create a routine and use it in the places that I was going to export.

        With PERL, there's always a way...

        Thanks for every ones help.