SQL:如何解决这些错误?

时间:2016-04-21 01:47:32

标签: sql database sqlite insert

所以我必须循环遍历.dat文件的文件夹,提取数据并使用INSERT INTO将数据插入数据库。

以下是其中一个文件的pastebin,用于查看我正在使用的数据:

http://pastebin.com/dn4wQjjE

要运行我刚才调用的脚本:

populate_database.sh directoryWithDatFiles

populate_database.sh脚本的内容:

rm test.sql;
sqlite3 test.sql "CREATE TABLE HotelReviews (HotelID SMALLINT, ReviewID SMALLINT, Author CHAR, Content CHAR, Date CHAR, Readers SMALLINT, HelpfulReviews SMALLINT, Over$

IFS=$'\n'
for file in $1/*;
do
    author=($(grep "<Author>" $file | sed 's/<Author>//g'));
    content=($(grep "<Content>" $file | sed 's/<Content>//g'));
    date=($(grep "<Date>" $file | sed 's/<Date>//g'));
    readers=($(grep "<No. Reader>" $file | sed 's/<No. Reader>//g'));
    helpful=($(grep "<No. Helpful>" $file | sed 's/<No. Helpful>//g'));
    overall=($(grep "<Overall>" $file | sed 's/<Overall>//g'));
    value=($(grep "<Values>" $file | sed 's/<Value>//g'));
    rooms=($(grep "<Room>" $file | sed 's/<Room>//g'));
    location=($(grep "<Location>" $file | sed 's/<Location>//g'));
    cleanliness=($(grep "<Cleanliness>" $file | sed 's/<Cleanliness>//g'));
    receptionarea=($(grep "<Check in / front desk>" $file | sed 's/<Check in \/ front desk>//g'));
    service=($(grep "<Service>" $file | sed 's/<Service>//g'));
    businessservice=($(grep "<Business service>" $file | sed 's/<Business service>//g'));

    length=${#author[@]}
    hotelID="$(echo $file | sed 's/.dat//g' | sed 's/[^0-9]*//g')";

    for((i = 0; i < length; i++)); do
            sqlite3 test.sql "INSERT INTO HotelReviews VALUES($hotelID, $i, 'author', 'content', 'date', ${readers[i]}, ${helpful[i]}, ${overall[i]}, 9, 10, ${location[i]}, ${cleanliness[i]}, ${receptionarea[i]}, ${service[i]}, ${businessservice[i]})";
    done

done

sqlite3 test.sql "SELECT * FROM HotelReviews;"

我遇到的问题是,虽然大部分脚本都在运行,但我仍然无法使用15列中的5列。我只是截取了我在尝试更改代码时遇到的错误:

'author' --> ${author[i]}: http://i.imgur.com/zKQLSqT.jpg
'content' --> ${content[i]}: http://i.imgur.com/pnirIo3.jpg
'date' --> ${date[i]}: http://i.imgur.com/urF5DTa.jpg 
9 --> ${value[i]}: http://i.imgur.com/AnBFSWp.jpg
10 --> ${rooms[i]}: same errors as above

无论如何,如果有人可以帮助我,我会非常感激。

干杯!

1 个答案:

答案 0 :(得分:0)

如果您处理大量XML,我建议您了解一个SAX解析器,例如Python标准库中的解析器。任何愿意编写像这样的shell脚本的人都有学习它的东西,结果将更容易阅读,至少有正确的祷告。

如果您想坚持使用正则表达式黑客攻击,请转到 awk 。使用“&gt;”作为您的字段分隔符,您的脚本可以使用像

这样的awk行进行简化
/<Author>/ { gsub(/'/, "''", $2); author=$2 }
/<Content>/ { gsub(/'/, "''", $2); content=$2 }
...
END { print author, content, ... }

gsub通过将数据中的任何单引号加倍来处理SQL引用问题。