将逗号分隔的字符串从管道导入到mysql db

时间:2017-04-24 19:35:19

标签: mysql bash csv sed

我需要插入以下逗号将来自stdout(speedtest-cli)的分隔字符串插入到mysql db中

1746,Vodafone DE,Frankfurt,2017-04-24T16:00:13.708464,292.1655728839447,267.019,119047915.27025849,12007236.796291795

列描述是: SERVER_ID,SPONSOR,SERVER_NAME,时间戳,DISTANCE,PING,下载,上载

在不需要将数据保存到csv然后将其导入mysql的情况下,完成此操作的最佳方法是什么?

我能够将逗号分隔的数据转换为sqlable数据

SQL_RESULT_DATA=$(echo $RESULT_DATA | sed -e "s/,/','/g;s/^\(.*\)$/'\1'/")

mysql --host=10.8.0.18 --user speedtest --password=speedtest < echo "INSERT INTO `TEST_DATA` (`SERVER_ID`, `SPONSOR`, `SERVER_NAME`, `TIMESTAMP`, `DISTANCE`, `PING`, `DOWNLOAD`, `UPLOAD`) VALUES ( $SQL_RESULT_DATA )"

失败
-bash: TEST_DATA: command not found
-bash: SERVER_ID: command not found
-bash: SPONSOR: command not found
-bash: SERVER_NAME: command not found
-bash: TIMESTAMP: command not found
-bash: DISTANCE: command not found
-bash: PING: command not found
-bash: DOWNLOAD: command not found
-bash: UPLOAD: command not found
-bash: echo: No such file or directory

表格说明:

MySQL [speedtest]> DESCRIBE TEST_DATA
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| TEST_ID     | int(11) | NO   | PRI | NULL    | auto_increment |
| SERVER_ID   | int(11) | NO   |     | NULL    |                |
| SPONSOR     | text    | NO   |     | NULL    |                |
| SERVER_NAME | text    | NO   |     | NULL    |                |
| TIMESTAMP   | text    | NO   |     | NULL    |                |
| DISTANCE    | int(11) | NO   |     | NULL    |                |
| PING        | int(11) | NO   |     | NULL    |                |
| DOWNLOAD    | int(11) | NO   |     | NULL    |                |
| UPLOAD      | int(11) | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:0)

让我在你的测试字符串上解释你的错误,

mysql --host=10.8.0.18 --user speedtest --password=speedtest < echo

表示“从名为echo的文件中读取stdin”

"“以字符串开头另一个mysql参数,此参数中包含空格”

INSERT INTO `TEST_DATA`

“执行命令TEST_DATA并将其输出插入此字符串”

(`SERVER_ID`, `SPONSOR`, `SERVER_NAME`, `TIMESTAMP`, `DISTANCE`, `PING`, `DOWNLOAD`, `UPLOAD`) VALUES ( $SQL_RESULT_DATA )"

包含与上面相同的错误,因为后面的刻度用于生成子shell并得到它的结果

你想要使用的是-e mysql命令的参数来传递查询,并且在该查询中你需要转义反引号,如

mysql --host=10.8.0.18 --user speedtest --password=speedtest -e "INSERT INTO \`TEST_DATA\` (\`SERVER_ID\`, \`SPONSOR\`, \`SERVER_NAME\`, \`TIMESTAMP\`, \`DISTANCE\`, \`PING\`, \`DOWNLOAD\`, \`UPLOAD\`) VALUES ( $SQL_RESULT_DATA )"

另见:

确保引用SQL_RESULT_DATA表达式,如下所示

SQL_RESULT_DATA="$(echo $RESULT_DATA | sed -e "s/,/','/g;s/^\(.*\)$/'\1'/")"

因为否则可能会导致将来出现令人讨厌的通配和分词问题,这可能会导致安全问题,请参阅https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells