【问题标题】:How to insert multiline data in mysql database如何在mysql数据库中插入多行数据
【发布时间】:2016-06-23 04:03:35
【问题描述】:

我的脚本中有一个类似这样的命令

MESSAGE=`grep -Po 'MSG.\K[\w\s,.'\'',.:]*' < $FILENAME`

现在当这个命令运行时,我的输出看起来像这样

Kohl's EDI will be down for scheduled maintenance starting at 12:30 am until
approximately 4:00 am central time on Wednesday June 22nd. Kohl's will not be
able to send or receive EDI or AS2 transmissions during this time. If your
company's AS2 software has an automated process to resend a file after a
failure, Kohl's encourages your company to enable the resend process. This     is
also a reminder for AS2 trading partners that Kohl's AS2 certificate will be
changing at 11:00 am central time on Tuesday June 21st.

现在在 grep 之后,我会将命令的结果传递给将要使用的变量,以便我可以将结果存储到 mysql 数据库中

问题是我该怎么做?

【问题讨论】:

  • $LINE 包含文件的名称,或者您要在其中 grep 的数据?
  • 是的 @anishsane 它包含我想要 grep 的文件的名称
  • 那么你的机制是正确的。您面临什么问题?
  • @anishsane 我在将它插入数据库时​​遇到了问题。我认为它不会插入到数据库中,因为我存储在变量中的是多行。如何将结果存储在一个变量中,以便将其插入数据库
  • 好的,那么你的问题是 "如何在 mysql 数据库中插入多行数据",而不是 "如果结果有,我如何将 grepped 结果存储到变量中多行?”;因为你正确地做后者。

标签: mysql linux bash unix


【解决方案1】:

确保您已从服务器连接到 mysql,然后您可以在插入语句中将 MEASSGE 变量传递为 \"$MESSAGE\" - \ 是因为在双引号中为有效的插入语句包装消息。

测试:我没有足够大的列来将整个消息存储为您的,所以我将其稍微修剪以适应列:

> MESSAGE="Kohl's EDI will be down for scheduled maintenance starting at 12:30 am until
approximately 4:00 am central time on Wednesday June 22nd. Kohl's will not be
able to send or receive EDI or AS2 transmissions during this time. If your
company's AS2 software has an automated process to resend a file after a
"
> sql "insert into at_test_run (run_id,run_error) values ('10000111',\"$MESSAGE\");"
> sql "select run_id,run_error from at_test_run where run_id='10000111'"
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| run_id   | run_error                                                                                                                                                                                                                                                                                                       |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 10000111 | Kohl's EDI will be down for scheduled maintenance starting at 12:30 am until
approximately 4:00 am central time on Wednesday June 22nd. Kohl's will not be
able to send or receive EDI or AS2 transmissions during this time. If your
company's AS2 software has an automated process to resend a file after a
 |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 2022-08-11
    • 2011-08-06
    • 2020-03-09
    • 2019-08-17
    相关资源
    最近更新 更多