LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   adding data to mysql database ? (https://www.linuxquestions.org/questions/programming-9/adding-data-to-mysql-database-408885/)

rharris72 01-28-2006 05:32 AM

adding data to mysql database ?
 
is it possible for bash to connect to a remote dbase and enter details from users inputs to it

the script i have that wish to add data to database is this

Code:

#!/bin/bash

ip=`cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep IPADDR|head -n1|cut -d'=' -f2`

echo "enter name"
read name
echo

echo "Enter Contact Address"
read letter
echo

echo "Please enter date Payment is due on"
read date
echo

echo "Please enter day of month to send Reminder"
read day
echo

echo "0 20 $day * * /home/archive/mailshot/sendmail -f sales@blah.co.uk -t "$letter" -m "Please be aware that payment for your server is due to us by the $date of this month" -u "$name Server Payment Due"" > /home/archive/mailshot/1.txt
echo "0 20 $day * * /home/archive/mailshot/sendmail -f sales@blah.co.uk -t "jerry@blah.co.uk" -m "$name on box $ip are due a payment by the $date of this month" -u "$name Server Payment Due"" > /home/archive/mailshot/mailshot/2.txt
echo "0 20 $day * * /home/archive/mailshot/sendmail -f sales@blah.co.uk -t "phil@blah.co.uk" -m "$name on box $ip are due a payment by the $date of this month" -u "$name Server Payment Due"" > /home/archive/mailshot/3.txt
echo "0 20 $day * * /home/archive/mailshot/sendmail -f sales@blah.co.uk -t "richie@blah.co.uk" -m "$name on box $ip are due a payment by the $date of this month" -u "$name Server Payment Due"" > /home/archive/mailshot/4.txt

a=`cat /home/archive/mailshot/1.txt`
b=`cat /home/archive/mailshot/2.txt`
c=`cat /home/archive/mailshot/3.txt`
d=`cat /home/archive/mailshot/4.txt`
z=`cat /home/archive/mailshot/mail.cron`

# make Filedescriptor(FD) 6 a copy of stdout (FD 1)
exec 6>&1

# open file "cron.txt" for writing
exec 1>/home/archive/mailshot/mail.cron

# produce some content
echo "$z"
echo "$a"
echo "$b"
echo "$c"
echo "$d"

# close file "mail.cron"
exec 1>&-

# make stdout a copy of FD 6 (reset stdout)
exec 1>&6

# close FD6
exec 6>&-

# make Filedescriptor(FD) 6 a copy of stdout (FD 1)
exec 6>&1

y=`cat /home/archive/mailshot/name.txt`

# open file "name.txt" for writing
exec 1>/home/archive/mailshot/name.txt

# produce some content
echo "$y"
echo "$name"

# close file "name.txt"
exec 1>&-

# make stdout a copy of FD 6 (reset stdout)
exec 1>&6

# close FD6
exec 6>&-

sed /^$/d mail.cron > mail1.cron
rm -rf mail.cron
mv mail1.cron mail.cron

crontab /home/archive/mailshot/mail.cron

rm -rf /home/archive/mailshot/1.txt
rm -rf /home/archive/mailshot/2.txt
rm -rf /home/archive/mailshot/3.txt
rm -rf /home/archive/mailshot/4.txt

exit 0


so it would add the clan contact address and repayment date

then if person was to end contract i have a scipt to remove the email reminder but would also like to connect to the dbase and remove details .

thanks in advance

ebonhand 01-30-2006 04:30 AM

Hmm.. I'm no guru, but I'm pretty sure you could cat into existence a file containing the required sql statements, and then pass the file to mysql using a command-line similar to:

Code:

mysql -uuser -ppassword db_name <sqlstatements.sql
Hope this helps!

- Ebonhand

smurff 01-30-2006 04:53 AM

rharris, I cant help but I just wanted to say, nice script :)

bigearsbilly 01-30-2006 05:36 AM

Quote:

Originally Posted by smurff
rharris, I cant help but I just wanted to say, nice script :)

????



Multiple echos instead of <<HERE documents, multiple hardcoded parameter strings.
Hardcoded directory paths?
It's one of those that develops into a hard to maintain
megascipt ;)

rharris72 01-30-2006 06:30 AM

please remeber i am new to linux so still learning

billy is it poss to connect to a remote database not on server to enter the info gathered into it

and then if client removes themselves from mailing i can delete database enteries ?

thanks in advance

bigearsbilly 01-30-2006 07:35 AM

sorry, i really wasn't trying to be rude ;)

remote database? Sure I should imagine so.
Most databases are on remote machines when you connect.
What sort of database?
You need to look at the DBMS software documentation
that should tell you how to do it.

I can't remeber a lot as I haven't done much with databases for a while (praise the lord!)

rharris72 01-30-2006 03:07 PM

billy i wasnt being funny im just keen to learn and all help is greatly recieved


All times are GMT -5. The time now is 04:42 PM.