[bash]insert data into table

#!/bin/bash


USER="root"

PASS="123456"

DATABASE="students"

TABLE="students"



if [ $# -ne 1 ];

then

        echo $0 DATAFILE

        echo

        exit 2

fi

data=$1


while read line;

do

        oldIFS=$IFS

        IFS=,

        values=($line)

        values[1]="\"`echo ${values[1]} | tr ' ' '#' `\""

        values[3]="\"`echo ${values[3]}`\""

        

        query=`echo ${values[@]} | tr ' #' ', ' `

        IFS=$oldIFS


mysql -u $USER -p$PASS $DATABASE <<EOF

        insert into $TABLE values($query);

EOF

done< $data


if [[ $? -eq 0 ]]; then

        echo Wrote data into DB

fi


你可能感兴趣的:([bash]insert data into table)