Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MySQL中插入值的Bash脚本_Mysql_Sql_Linux_Bash_Scripting - Fatal编程技术网

在MySQL中插入值的Bash脚本

在MySQL中插入值的Bash脚本,mysql,sql,linux,bash,scripting,Mysql,Sql,Linux,Bash,Scripting,我想制作一个bash脚本,连接到我的MySQL服务器,并从txt文件中插入一些值。 我已经写下了: #!/bin/bash echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('cat test.txt');" | mysql -uroot -ptest test; 但我收到了以下错误: 第1行出现错误1136(21S01):列计数与值计数不匹配 在第一排 我想这个错误在我的txt文件中,但我尝试了许多变体,仍然没有成功的希望 我的txt文件如下所示:

我想制作一个bash脚本,连接到我的MySQL服务器,并从txt文件中插入一些值。 我已经写下了:

#!/bin/bash
echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('cat test.txt');" | mysql -uroot -ptest test;
但我收到了以下错误:

第1行出现错误1136(21S01):列计数与值计数不匹配 在第一排

我想这个错误在我的txt文件中,但我尝试了许多变体,仍然没有成功的希望

我的txt文件如下所示:

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


假设您有许多行要添加,您可能需要语句,而不是
INSERT
。源文件必须在服务器上,但这里似乎是这样

诸如此类:

#!/bin/bash

mysql -uroot -ptest test << EOF

LOAD DATA INFILE 'test.txt'
    INTO TABLE tbl_name
    FIELDS TERMINATED BY ' ';

EOF
#/bin/bash

mysql-uroot-ptest test假设您有许多行要添加,您可能需要语句,而不是
INSERT
。源文件必须在服务器上,但这里似乎是这样

诸如此类:

#!/bin/bash

mysql -uroot -ptest test << EOF

LOAD DATA INFILE 'test.txt'
    INTO TABLE tbl_name
    FIELDS TERMINATED BY ' ';

EOF
#/bin/bash

mysql-uroot-ptest test您试图将值“cat test.txt”作为字符串插入数据库的insert语句中,该语句需要3个参数(IP、MAC和服务器),因此您会收到此错误消息

您需要首先读取文本文件并提取IP、MAC和服务器值,然后在查询中使用这些值,一旦填充,查询结果如下所示:

#!/bin/bash
echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('10.16.54.29', '00:f8:e5:33:22:3f', 'marsara');" | mysql -uroot -ptest test;

您试图将值“cat test.txt”作为字符串插入数据库中的insert语句中,该语句需要3个参数(IP、MAC和服务器),因此您会收到此错误消息

您需要首先读取文本文件并提取IP、MAC和服务器值,然后在查询中使用这些值,一旦填充,查询结果如下所示:

#!/bin/bash
echo "INSERT INTO test (IP,MAC,SERVER) VALUES ('10.16.54.29', '00:f8:e5:33:22:3f', 'marsara');" | mysql -uroot -ptest test;
试试这个:

#!/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;
通过这种方式,您可以读取文件并执行mysql命令。

尝试以下方法:

#!/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;

通过这种方式,您可以读取流式传输文件以及mysql comand执行。

我使用这种方式,它可以工作:

mysql -uroot -proot < infile
mysql-uroot-proot
或者先选择数据库

./mysql -uroot -proot db_name < infile
/mysql-uroot-proot db_name
或者将整个SQL复制到剪贴板并用

pbpaste > temp_infile && mysql -uroot -proot < temp_infile && rm temp_infile
pbpaste>temp\u infle&&mysql-uroot-proot
我使用它,它可以工作:

mysql -uroot -proot < infile
mysql-uroot-proot
或者先选择数据库

./mysql -uroot -proot db_name < infile
/mysql-uroot-proot db_name
或者将整个SQL复制到剪贴板并用

pbpaste > temp_infile && mysql -uroot -proot < temp_infile && rm temp_infile
pbpaste>temp\u infle&&mysql-uroot-proot
在AWS上通过ssh工作时,我遇到了一个问题—使用
加载数据本地填充
对我有效。在AWS上通过ssh工作时,我遇到了一个问题—使用
加载数据本地填充
对我有效。您好,欢迎使用SO。由于这个问题有几个其他的被投票的答案,请考虑添加一个描述你的代码是如何不同或对其他方法的改进。由于这个问题有几个其他被否决的答案,请考虑添加一个描述代码如何不同或对其他方法的改进。