使用shell脚本将数据插入到远程MySQL数据库 [英] Using shell script to insert data into remote MYSQL database

查看:1216
本文介绍了使用shell脚本将数据插入到远程MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图让一个shell(bash)的脚本插入一行到远程数据库,但我已经遇到了一些麻烦:(

I've been trying to get a shell(bash) script to insert a row into a REMOTE database, but i've been having some trouble :(

该脚本旨在将文件上载到服务器,得到一个URL,HASH和文件大小,连接到远程MySQL数据库,并将数据插入到现有的表。我已经得到了它的工作,直到远程MySQL数据库位。

The script is meant to upload a file to a server, get a URL,HASH, and a filesize, connect to a remote mysql database, and insert the data into an existing table. I've gotten it working until the remote MYSQL database bit.

它看起来是这样的:

#!/bin/bash

zxw=randomtext
description=randomtext2

for file in "$@"
 do
 echo -n *****
 ident= *****
 data= ****
 size=` ****
 hash=`****
mysql --host=randomhost --user=randomuser --password=randompass randomdb
insert into table (field1,field2,field3) values('http://www.site.com/$hash','$file','$size');
echo "done"
done

我在节目一共有小白所以叶:P

I'm a total noob at programming so yeh :P

不管怎样,我加入了\\逃脱括号内,因为我得到的错误。由于它是现在,剧本,直到连接到MySQL数据库工作正常。它只是连接到MySQL数据库和不执行INSERT命令(我甚至不知道如果INSERT命令将在bash工作)。

Anyway, I added the \ to escape the brackets as I was getting errors. As it is right now, the script is works fine until connects to the mysql database. It just connects to the mysql database and doesn't do the insert command (and i dont even know if the insert command would work in bash).

PS:我一个试用过的mysql命令,在命令行之一,而他们的工作,虽然我所定义的哈希/文件/大小,并没有逃逸\\

PS: I've tried both the mysql commands from the command line one by one, and they worked, though I defined the hash/file/size and didn't have the escaping "\".

总之,你们觉得呢?是林试图做甚至可能吗?如果是的话怎么样?

Anyway, what do you guys think? Is what im trying to do even possible? If so how?

任何帮助将是AP preciated:)

Any help would be appreciated :)

推荐答案

insert语句必须发送到MySQL,在shell脚本中没有另一条线路,所以你需要使它成为一个在这里的文件。

The insert statement has to be sent to mysql, not another line in the shell script, so you need to make it a "here document".

mysql --host=randomhost --user=randomuser --password=randompass randomdb << EOF
insert into table (field1,field2,field3) values('http://www.site.com/$hash','$file','$size');
EOF

&LT;&LT; EOF 指含有什么,但 EOF (开头没有空格)作为标准输入到程序中的下一行之前采取一切。

The << EOF means take everything before the next line that contains nothing but EOF (no whitespace at the beginning) as standard input to the program.

这篇关于使用shell脚本将数据插入到远程MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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