carlosp Posted March 30, 2013 Share Posted March 30, 2013 (edited) 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 March 30, 2013 by carlosp Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/ Share on other sites More sharing options...
trq Posted March 30, 2013 Share Posted March 30, 2013 As is clearly stated in the shebang, that code is Perl not Bash. Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421886 Share on other sites More sharing options...
carlosp Posted March 30, 2013 Author Share Posted March 30, 2013 Thanks for correction, i'm new to this ground, where i've found the script it said Bash, this is why i thought it's a Bash script.... Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421928 Share on other sites More sharing options...
jazzman1 Posted March 30, 2013 Share Posted March 30, 2013 No, it's Perl. What database is used for? Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421936 Share on other sites More sharing options...
carlosp Posted March 30, 2013 Author Share Posted March 30, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421938 Share on other sites More sharing options...
jazzman1 Posted March 30, 2013 Share Posted March 30, 2013 Do you want to give you an example in bash? Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421943 Share on other sites More sharing options...
carlosp Posted March 30, 2013 Author Share Posted March 30, 2013 well i figured how to export the xml file and the correct format, it was very simple, but when you're new to this looks impossible script.sh > myXMLfile.xml thanks for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421945 Share on other sites More sharing options...
jazzman1 Posted March 30, 2013 Share Posted March 30, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/276306-bash-script-to-create-xml-from-mysql-query-out-of-memory/#findComment-1421950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.