bash脚本插入值的MySQL [英] Bash script to insert values in MySQL

查看:98
本文介绍了bash脚本插入值的MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个连接到我的MySQL服务器并插入要从一个txt文件的一些valuse bash脚本。
我已经写了下来:

 #!/斌/庆典
回声INSERT INTO测试(IP,MAC,SERVER)VALUES('猫的test.txt'); | MySQL的-uroot -ptest试验;

但我recieving以下错误:


  

ERROR 1136(21S01)位于第1行:列计数不匹配值计数
  在行1


我猜想错误是在我的txt文件,但我尝试了许多变化,仍然没有成功的希望。

我的tx​​t文件看起来是这样的:


  

10.16.54.29 00:F8:E5:33:22:3F marsara



解决方案

试试这个:

 #!/斌/庆典
inputfile中=的test.txt
猫$ inputfile中|同时读取IP MAC服务器;做
    回声INSERT INTO测试(IP,MAC,SERVER)VALUES('$ IP,$ MAC,$服务器');
做| MySQL的-uroot -ptest试验;

在流媒体文件通过这种方式读取以及MySQL的COMAND执行。

I want to make a bash script that connects to my MySQL server and inserts some valuse from a txt file. I have written this down:

#!/bin/bash
echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('cat test.txt');" | mysql -uroot -ptest test;

but I'm recieving the following error:

ERROR 1136 (21S01) at line 1: Column count doesn't match value count at row 1

I suppose the error is in my txt file, but I've tried many variations and still no hope of success.

My txt file looks like this:

10.16.54.29 00:f8:e5:33:22:3f marsara

解决方案

Try this one:

#!/bin/bash
inputfile="test.txt"
cat $inputfile | while read ip mac server; do
    echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('$ip', '$mac', '$server');"
done | mysql -uroot -ptest test;

This way you streaming the file read as well the mysql comand execution.

这篇关于bash脚本插入值的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆