[英]bash scripting variable in SQL file
I want to read a barcode with a scanner and save the code in a MySQL Database. 我想用扫描仪读取条形码并将代码保存在MySQL数据库中。 The Barcode Scanner is connected to a Raspberry Pi and works like a keyboard.
条形码扫描仪已连接到Raspberry Pi,并且像键盘一样工作。 I have written two files: A Bash script to read the data from the code (input) and connect to the database (Login.sh) and also a SQL file to insert the data in the database (insert.sql)
我已经编写了两个文件:一个Bash脚本,用于从代码(输入)读取数据并连接到数据库(Login.sh),还有一个SQL文件,用于将数据插入数据库(insert.sql)。
The database connection, read of the input and insert in the database works sucessful. 数据库连接,读取输入并插入数据库成功。 Can i save the input in a variable and use it in the SQL file?
我可以将输入保存在变量中并在SQL文件中使用它吗?
Login.sh: Login.sh:
read code;
mysql -p host=10.0.8.3 --user=root --password=secret databasename < insert.sql
insert.sql: insert.sql:
INSERT INTO data (id) VALUES ('$code')
Thank you so much for any answer 非常感谢您的任何答复
Why not just go 为什么不随便走
read code;
echo "INSERT INTO data (id) VALUES ('$code')" | mysql -p host=192.168.1.2 --user=root --password=secret databasename
If you really want to use $code
in the insert script, you should 如果您确实要在插入脚本中使用
$code
,则应
export code
in the login script after read code
. 在登录脚本中
read code
。 But that will make $code
available globally in the current shell - probably not a good idea. 但这将使
$code
在当前Shell中全局可用-可能不是一个好主意。
Cheers, 干杯,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.