嵌入式数据库sqlite csv 操作shell脚本
嵌入式数据sqlite csv 操作shell脚本
·
#!/bin/bash
# The GPLv3 License (GPLv3)
#
# Copyright (c) 2022 HongDaYu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
#Maybe check database before run main driver
#
FILE_PATH=$1
TMP=${IFS}
SRC_TMP=""
INDEX=0
BAK_FILE=${FILE_PATH}.$(date +%Y-%m-%d).bak
#TODO check database table
TABLENAME=$(echo ${FILE_PATH} | awk -F'.' '{print $1}')
echo "chech table ${TABLENAME} ... ..."
[[ -f ${BAK_FILE} ]] && rm -rf ${BAK_FILE}
echo "$(date +%Y-%m-%d): create bak file ${BAK_FILE}"
cp -rf ${FILE_PATH} ${BAK_FILE}
if ! [[ $# -eq 2 ]]; then
echo "$0 [csv name] [dbname]"
exit 1
fi
if [[ -f ${FILE_PATH} ]]; then
while read -u4 index; do
IFS=,
for index_csv in ${index}; do
CNTS=$(sqlite3 $2 "SELECT COUNT(*) FROM rau WHERE ID='${index_csv}'")
if [[ ${CNTS} -gt 0 ]]; then
echo "${index_csv} had exist in rau table"
sed -i '/'${index_csv}'/d' ${BAK_FILE}
fi
break
IFS=${TMP}
done
done 4<${FILE_PATH}
fi
sed -i '/^[[:space:]]*$/d' ${BAK_FILE}
#TODO CHECK EVERY INDEX IN database table
#
#
if ! [[ -f ${BAK_FILE} ]]; then
echo "$(date +%Y-%m-%d) not exist in rau table"
fi
#SQL OPERATOR DATABASE
[[ -f ${BAK_FILE} ]] && sqlite3 <<-EOF
.open $2
.mode csv
.import ${BAK_FILE} rau
EOF
if [[ $? -eq 0 ]]; then
echo "create datebase ok ... ..."
else
echo "create database not ok ... ..."
fi
更多推荐
所有评论(0)