简体   繁体   中英

insert values in mysql with bash script

How can i connect to my db and execute the query in bash

this is my code so far:

echo "estacion: "$st;
fcha=$year2"-"$month"-"$day;
echo "fecha: "$fcha;

echo $archivoF " ==> " $rutabase"datos/obs/"$st"/"$year2"/"$archivoF;
if [ ! -d $rutabase"datos/obs/"$st"/"$year2 ]; then
  mkdir -p $rutabase"datos/obs/"$st"/"$year2;
fi
mv $archivoF $rutabase"datos/obs/"$st"/"$year2
IFS='.' read -ra tipoO <<< "$archivoF"
tipoOb=`echo "."${tipoO[1]}"."${tipoO[2]}`
query="insert into FILES (name,type,date,station) VALUES($archivoF,$tipoOb,$fcha,$st)"

echo $archivoG " ==> "$rutabase"rinex/nav/"$st"/"$year2"/"$archivoG;
if [ ! -d $rutabase"datos/nav/"$st"/"$year2 ]; then
mkdir -p $rutabase"datos/nav/"$st"/"$year2;
fi
mv $archivoG $rutabase"datos/nav/"$st"/"$year2
IFS='.' read -ra tipoN <<< "$archivoG"
tipoNa=`echo "."${tipoN[1]}`
query="insert into FILES (name,type,date_f,station) VALUES($archivoG,$tipoOb,$fcha,$st)"

any suggestions

To execute a query from a script, use the mysql command with the -e option.

query="insert into FILES (name,type,date,station) VALUES('$archivoF','$tipoOb','$fcha','$st')"
mysql -h dbhost -u dbuser -ppassword dbname -e "$query"

Make sure you put quotes around the values that are strings in the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM