I want to do something like this:
我想做这样的事情:
cat files_list | cut -d' ' -f1,3 | mysql -uroot -pxxxx -e "insert into table(var1, var2) values($f1,$f2)"
How can i achieve this thing?
我怎样才能实现这个目标?
1 个解决方案
#1
1
You can use the following bash
script:
您可以使用以下bash脚本:
while read -a record ; do
mysql -uroot -pxxxx -e "insert into table(var1, var2) values(${record[0]}, ${record[2]})"
done < files_list
However, this is simple but performs not very well.
然而,这很简单但表现不是很好。
If the task is performance critical, I would build just a single mysql query, which inserts all rows at once, or even better: use LOAD_DATA_INFILE
如果任务是性能关键的,我只会构建一个mysql查询,它会一次性插入所有行,甚至更好:使用LOAD_DATA_INFILE
Also note, if the task is security critical, meaning the input data comes from an untrusted source, I wouldn't use the command line mysql client at all. Using a programming language which supports prepared statements for mysql - like PHP - would be the way to go.
另请注意,如果任务是安全关键的,意味着输入数据来自不受信任的源,我根本不会使用命令行mysql客户端。使用支持mysql的预处理语句的编程语言(如PHP)将是最佳选择。
Using a programming language would had another important advantage - you wouldn't need to pass the password via commandline which is insecure.
使用编程语言会有另一个重要的优点 - 您不需要通过命令行传递密码,这是不安全的。
#1
1
You can use the following bash
script:
您可以使用以下bash脚本:
while read -a record ; do
mysql -uroot -pxxxx -e "insert into table(var1, var2) values(${record[0]}, ${record[2]})"
done < files_list
However, this is simple but performs not very well.
然而,这很简单但表现不是很好。
If the task is performance critical, I would build just a single mysql query, which inserts all rows at once, or even better: use LOAD_DATA_INFILE
如果任务是性能关键的,我只会构建一个mysql查询,它会一次性插入所有行,甚至更好:使用LOAD_DATA_INFILE
Also note, if the task is security critical, meaning the input data comes from an untrusted source, I wouldn't use the command line mysql client at all. Using a programming language which supports prepared statements for mysql - like PHP - would be the way to go.
另请注意,如果任务是安全关键的,意味着输入数据来自不受信任的源,我根本不会使用命令行mysql客户端。使用支持mysql的预处理语句的编程语言(如PHP)将是最佳选择。
Using a programming language would had another important advantage - you wouldn't need to pass the password via commandline which is insecure.
使用编程语言会有另一个重要的优点 - 您不需要通过命令行传递密码,这是不安全的。