Register
It is currently Fri Nov 28, 2014 4:49 pm

Import xml in mysql using bashscripting


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Apr 10, 2013 6:06 am   

Joined: Wed Apr 10, 2013 5:43 am
Posts: 1
Hi,
I'm not a pro bashscript writer but I'm learning and want to learn about my mistakes.
In the next script I have an error on rule 6 but I can't find what I'm doing wrong ...
I daily receive a file xml.xml and have to import it in an mysql database in a few existing tables.

Code:
#!/bin/bash
FILE="/transfer/store/xml.xml"
RESULT=$(xmlstarlet sel -t -v "count(//Appointment)" $FILE)
_DB_TABLE_PERSONS="persons"
_DB_TABLE_INVITATIONS="invitiations"
FOR (( i=0; i<$RESULT; i++ )) ; DO
PFIRST_NAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data1 $FILE)"
PLAST_NAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data2 $FILE)"
PADDRESS="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data3 $FILE)"
PTELEPHONE="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data4 $FILE)"
PEMAIL="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/email $FILE)"
VALUES(NULL,'${FIRST_NAME}','${LAST_NAME}','${ADDRESS}','${TELEPHONE}','${EMAIL}');"
QUERY="INSERT INTO ${_DB_TABLE_PERSONS} ('id','first_name','last_name','address','telephone','email') VALUES(NULL,'${PFIRST_NAME}','${PLAST_NAME}','${PADDRESS}','${PTELEPHONE}','${PEMAIL}');"
mysql --host=10.1.12.69 --user=test --password=test --database=test < $QUERY
ID_PERSONS=$(mysql --host=10.1.12.69 --user=test --password=test --database=test -se "SELECT LAST_INSERT_ID()")
IREMARK="$(xmlstarlet sel -t -v //Appointment[${i}+1]/person-info/data5 $FILE)"
ELAST_NAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/transfer-info/name $FILE)"
ID_EMPLOYEES=$(mysql --host=10.1.12.69 --user=test --password=test --database=test -se "SELECT id FROM employees WHERE last_name=${ELAST_NAME}")
LNAME="$(xmlstarlet sel -t -v //Appointment[${i}+1]/transfer-info/locatiebenaming $FILE)"
ID_LOCATIONS=$(mysql --host=10.1.12.69 --user=test --password=test --database=test -se "SELECT id FROM locations WHERE UCASE(name) like '%${ELAST_NAME}%'")
ID_I_TEMP="$(xmlstarlet sel -t -v //Appointment[${i}+1]/Date $FILE)"
IT_I_TEMP="$(xmlstarlet sel -t -v //Appointment[${i}+1]/Time/Begin $FILE)"
IDATE_INVITATION=${ID_I_TEMP:0:4}-${ID_I_TEMP:4:2}-${ID_I_TEMP:6:2}
IEXPIRATION_DATE=${IT_I_TEMP:0:2}-${IT_I_TEMP:2:2}
QUERY="INSERT INTO ${_DB_TABLE_INVITATIONS} ('id','person','employee','location','date_invitation','time_invitation','notification','remark') VALUES(NULL,'${PFIRST_NAME}','${PLAST_NAME}','${PADDRESS}','${PTELEPHONE}','${PEMAIL}');"
mysql --host=10.1.12.69 --user=test --password=test --database=test < $QUERY
DONE


XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<transfer-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Appointment>
   <Date>20130409</Date>
   <Time>
     <Begin>1830</Begin>
     <End>1900</End>
   </Time>
   <person-info>
     <data1>bla</data1>
     <data2>m00</data2>
     <data3>street 10</data3>
     <data4>0000/000000</data4>
     <data5>subject</data5>
     <info>remarks</info>
     <email></email>
     <sms></sms>
   </person-info>
   <transfer-info>
     <dcode>BARBRE5</dcode>
     <name>TESTPERSON</name>
     <locatiebenaming>TESTLOCATION</locatiebenaming>
     <locatieID>3702</locatieID>
     <entryid>45784139</entryid>
   </transfer-info>
</Appointment>
<Appointment>
   <Date>20130412</Date>
   <Time>
     <Begin>1600</Begin>
     <End>1730</End>
   </Time>
   <person-info>
     <data1>bla2</data1>
     <data2>m002</data2>
     <data3>street 666</data3>
     <data4>0000000000</data4>
     <data5>andere</data5>
     <info></info>
     <email>mailaddress2</email>
     <sms></sms>
   </person-info>
   <transfer-info>
     <dcode>BIRBRE5</dcode>
     <name>TESTPERSON2</name>
     <locatiebenaming>TESTLOCATION2</locatiebenaming>
     <locatieID>5802</locatieID>
     <entryid>4578487</entryid>
   </transfer-info>
</Appointment>
</transfer-export>


Top
 Profile  
 PostPosted: Thu Apr 11, 2013 5:44 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hi,

You cannot uppercase commands in that manner, line 6 should use 'for' rather than 'FOR' and similar for the 'DO' and 'DONE'.

also, this line should not be there;

VALUES(NULL,'${FIRST_NAME}','${LAST_NAME}','${ADDRESS}','${TELEPHONE}','${EMAIL}');"

I suggest that you make the script more human readable as then it will be easier to debug, this is quite hard to read through.

Also you do not include the error message so we have to guess :)

DW


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP