Jump to content

bash script to create xml from mysql query - out of memory


Recommended Posts

i need to generate an xml file from database records, and i get the error out of memory...
here's the script i am using, it's found on google, and it's not suitable for me...
and it's also killing the server allocated memory... (but it's a beginning)

 

    #!/usr/bin/perl

    use warnings;
    use strict;
    use XML::Simple;
    use DBI;

    my $dbh = DBI->connect('DBI:mysql:db_name;host=host_address','db_user','db_pass')
      or die DBI->errstr;

    # Get an array of hashes
    my $recs = $dbh->selectall_arrayref('SELECT * FROM my_table',{ Columns => {} });

    # Convert to XML where each hash element becomes an XML element
    my $xml = XMLout( {record => $recs}, NoAttr => 1 );

    print $xml;

    $dbh->disconnect;
 

 



this script only print the records, because i tested with an where clause for a single row id...

first of all i couldn't manage to make it to save the output to a file.xml (i have no idea how to do it)
second, i need somehow to split the "job" in multiple jobs and then put together the xml file all in one piece. (i have no idea, and couldn't find any info on google)

thanks for all your answers in advance!
PS: i don't have access to change server settings :(

Edited by carlosp
Link to comment
Share on other sites

mysql, i managed to make some fixes, now i'm stuck with xml output file

the problem was the query which overloads the server allocated memory

changed to this:

 

print "<users>\n";
my $sth = $dbh->prepare("SELECT id ,name, email, phone, etc, etc, etc,  FROM my_table  ORDER BY id DESC");
$sth->execute;

   while (my ($id ,$name, $email, $phone, $etc, $etc, $etc,) = $sth->fetchrow_array ()) {
       print " <user>\n";
       print "  <user_id>$id</user_id>\n";
       print "  <name><![CDATA[$name]]></name>\n";
       print "  <email>$email</email>\n";
       print "  <phone>$phone</phone>\n"; 
       $etc ....
}
print "</users>\n";

$dbh->disconnect;
 

now i don't know how to write the output to an xml file, tried some scripts from google but without success...

Link to comment
Share on other sites

There is too much code, just use --xml or --html flags to retrieve the content from tables and write it down into a file.

 

Example in Bash with a xml flag:

 

#!/bin/bash

# MYSQL DATABASE Login info
DBHOST=localhost
DBUSER=jazzman
DBPASS=password

# define the database and select the table 
CMD="use sakila;SELECT * FROM film WHERE release_year=2006;"

/usr/bin/mysql -h ${DBHOST} -u ${DBUSER} --password=${DBPASS} --xml -e "$CMD">~/newXML.xml

 Results:

 

<?xml version="1.0"?>

<resultset statement="SELECT * FROM film WHERE release_year=2006" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
	<field name="film_id">1</field>
	<field name="title">ACADEMY DINOSAUR</field>
	<field name="description">A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies</field>
	<field name="release_year">2006</field>
	<field name="language_id">1</field>
	<field name="original_language_id" xsi:nil="true" />
	<field name="rental_duration">6</field>
	<field name="rental_rate">0.99</field>
	<field name="length">86</field>
	<field name="replacement_cost">20.99</field>
	<field name="rating">PG</field>
	<field name="special_features">Deleted Scenes,Behind the Scenes</field>
	<field name="last_update">2006-02-15 05:03:42</field>
  </row>

  <row>
	<field name="film_id">2</field>
	<field name="title">ACE GOLDFINGER</field>
	<field name="description">A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China</field>
	<field name="release_year">2006</field>
	<field name="language_id">1</field>
	<field name="original_language_id" xsi:nil="true" />
	<field name="rental_duration">3</field>
	<field name="rental_rate">4.99</field>
	<field name="length">48</field>
	<field name="replacement_cost">12.99</field>
	<field name="rating">G</field>
	<field name="special_features">Trailers,Deleted Scenes</field>
	<field name="last_update">2006-02-15 05:03:42</field>
  </row>

  <row>
	<field name="film_id">3</field>
	<field name="title">ADAPTATION HOLES</field>
	<field name="description">A Astounding Reflection of a Lumberjack And a Car who must Sink a Lumberjack in A Baloon Factory</field>
	<field name="release_year">2006</field>
	<field name="language_id">1</field>
	<field name="original_language_id" xsi:nil="true" />
	<field name="rental_duration">7</field>
	<field name="rental_rate">2.99</field>
	<field name="length">50</field>
	<field name="replacement_cost">18.99</field>
	<field name="rating">NC-17</field>
	<field name="special_features">Trailers,Deleted Scenes</field>
	<field name="last_update">2006-02-15 05:03:42</field>
  </row>
</resultset>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.