根据IDRow

时间:2017-03-06 11:29:45

标签: linux database bash sqlite csv

我有一个小脚本,它从表中获取值,然后放入csv文件中。问题是我无法根据IDRow发布多个csv文件。

if [ "$1" = "" ]
then
    echo "Database path"
    exit 1
fi

if [ "$2" = "" ]
then
    echo "Csv file output path"
exit 1
fi

db=$1
t=($(sqlite3 $db ".tables 'myTable'"))

for i in "${t[@]}"
do
    sqlite3 $db<<- EXIT_HERE
    .mode csv
    .separator ;
    .output $2/$i.csv
    SELECT IDRow,strftime('%d-%m-%Y %H:%M:%S',Date),Value FROM $i order by IDRow;
    .exit
    EXIT_HERE
    echo "$i.csv generated"
done

我在csv文件中得到的是:

  

IDRow;日期时间;价值
  3; 06-03-2017 10:53:40; 120
  3; 06-03-2017 10:53:41; 120
  4; 06-03-2017 11:41:22; 12000
  4; 06-03-2017 11:41:23; 12000
  5; 06-03-2017 11:51:49; 15000
  5; 06-03-2017 11:51:50; 15000

如何获得3个不同的文件?谢谢

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方案

if [ "$1" = "" ]
then
    echo "Database path"
    exit 1
fi

if [ "$2" = "" ]
then
    echo "Csv file output path"
exit 1
fi

db=$1
t=($(sqlite3 $db ".tables"))
row=(sqlite3 $1 "select IDRow from mytable group by IDRow")

for i in "${row[@]}"; do
    for j in "${table[@]}"; do
        sqlite3 $db<<- EXIT_HERE
        .mode csv
        .separator ;
        .output $2/$i.csv
         SELECT IDRow,Date,Value FROM $j order by IDRow=$i;
        .exit
        EXIT_HERE
    done
 done

谢谢