[bash]insert data into table

#!/bin/bashmysql


USER="root"sql

PASS="123456"bash

DATABASE="students"it

TABLE="students"query



if [ $# -ne 1 ];while

thendata

        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

相關文章
相關標籤/搜索