如何在bash脚本中使用参数执行SQL命令 [英] How to execute SQL command with parameters in bash script

查看:45
本文介绍了如何在bash脚本中使用参数执行SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个bash脚本,它将用户添加到我在MySQL数据库中的自定义表中.我将用户名作为脚本参数存储在变量$ USERNAME中,我需要在MySQL服务器中执行以下查询:

I would like to make a bash script which will add user to my custom table in MySQL database. I have username as script argument stored in variable $USERNAME and I need to execute this query into MySQL server:

INSERT INTO `user` VALUES ('$USERNAME', password)

我知道如何执行sql文件,但是当我在变量中使用用户名时,这对我没有帮助.我能想到的唯一方法是使用GET值执行php文件并在php中执行sql命令.而且我不确定php是否可以按我的想法工作.

I know how to execute a sql file but that won't help me when I have username in variable. The only way I can think of is to execute php file with GET values and execute sql command in php. And I'm not eve sure that with php will work as I think.

还有什么更好的方法吗?

Is there any better way?

谢谢

在您有帮助的答案之后,我使用了此命令,该命令有效:

After your helpfull answers I went with this command which works:

mysql -ppassword --default-character-set=utf8 -e "INSERT INTO table VALUES (\"$USERNAME@\",\"password\")" database

推荐答案

命令为:

mysql -pyourpasswordwithoutspaces -e "Your insert query goes here with semicolon;"

我不确定分号是否必要,但是提供分号总是很好.

I'm not sure if the semicolons are necessary or not, but it always good to provide them.

这篇关于如何在bash脚本中使用参数执行SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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