上交所行情文件导入数据库
事情的起因很简单,需要将股票收盘行情导入数据库,因为科创板交易时间延长,需要将原有的程序进行改造,众所周知,程序员永远是不够用的,只能自己解决这个问题。
方式是用定时器调用shell脚本。
上交所的mktdt00.txt使用“|”作为分隔符。
核心代码是将mktdt00.txt转成程insert into table values( ) 然后有sqlplus导入数据库。
代码一:
#!/bin/bash
dbfpath=/home/oracle/dbf_insert/stockkcb/dbf
IFS=$'\n'
for entry in $(cat $dbfpath/mktdt00.txt)
do
IFS='|'
arr=($entry)
if [ ${arr[0]} == 'MD001' ]; then ## index
a=" INSERT INTO mktdt00 VALUES( ""'"
a=$a"${arr[1]}""','""${arr[2]}""',"
a=$a"${arr[5]}"",""${arr[6]}"",""${arr[4]}"
a=$a",""${arr[7]}"",""${arr[8]}"",""${arr[9]}"",0,0,""${arr[3]}"
a=$a",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ) ; "
echo $a>>$dbfpath/mktdt00.sql
elif [ ${arr[0]} == 'MD002' ] || [ ${arr[0]} == 'MD003' ] || [ ${arr[0]} == 'MD004' ] ;
then
## stock bond fund
a=" INSERT INTO mktdt00 VALUES( ""'"
a=$a"${arr[1]}""','""${arr[2]}""',"
a=$a"${arr[5]}"",""${arr[6]}"",""${arr[4]}"",""${arr[7]}"",""${arr[8]}"
a=$a",""${arr[10]}"",""${arr[11]}"",""${arr[13]}"",""${arr[3]}"",0"
a=$a",""${arr[12]}"",""${arr[15]}"",""${arr[16]}"",""${arr[19]}"",""${arr[20]}"
a=$a",""${arr[14]}"",""${arr[17]}"",""${arr[18]}"",""${arr[21]}"",""${arr[22]}"
a=$a",""${arr[23]}"",""${arr[24]}"",""${arr[27]}"",""${arr[28]}"",""${arr[25]}"
a=$a",""${arr[26]}"",""${arr[29]}"",""${arr[30]}"" );"
echo $a>>$dbfpath/mktdt00.sql
else
echo "success!"
fi
done
echo "commit;">>$dbfpath/mktdt00.sql
'MD001' 是指数格式和 其他的格式有所不同。
代码二
#!/bin/sh
. ~oracle/.bash_profile
user='数据库用户名'
pwd='数据库密码'
dbfpath=/home/oracle/dbf_insert/stockkcb/dbf
cd $dbfpath
rm -rf *
wget http://内网ipip/mktdt00.txt #下载局域网中的交易所行情文件mktdt00.txt
sh $dbfpath/readkcb.sh #执行上一下txt转sql 脚本
if [ -s mktdt00.sql ];then
sqlplus $user/$pwd <<EOF >/tmp/imp.log
truncate table mktdt00;
@$dbfpath/mktdt00.sql
quit
EOF