mysql的“where子句”bash脚本中的未知列
这是我的控制台命令的第一行和输出:mysql的“where子句”bash脚本中的未知列,mysql,bash,Mysql,Bash,这是我的控制台命令的第一行和输出: source request.sh a a "ZP07" a a ZP07 USE intro_ict; SELECT idbuildings from buildings WHERE address = ZP07; mysql: [Warning] Using a password on the command line interface can be insecure. the tables in the database are:
source request.sh a a "ZP07"
a a ZP07
USE intro_ict; SELECT idbuildings from buildings WHERE address = ZP07;
mysql: [Warning] Using a password on the command line interface can be insecure.
the tables in the database are: buildings
room
sensordata
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1054 (42S22) at line 1: Unknown column 'ZP07' in 'where clause'
the selected building ZP07 has idbuilding
这是request.sh:
1 #!/bin/bash
2
3 #args = startdate ($1) enddate ($2) address($3) roomnr($4)
4 echo "$1 $2 $3 $4"
5
6 sql1="USE intro_ict; SELECT idbuildings from buildings WHERE address = $3;"
7 tables="USE intro_ict; SHOW TABLES;"
8 echo "$sql1"
9
10 echo "the tables in the database are: $(mysql -h 127.0.0.1 -P 3306 -u root -padmin -se "$tables")"
11 idbuilding=$(mysql -h 127.0.0.1 -P 3306 -u root -padmin -se "USE intro_ict; SELECT idBuildings FROM Buildings WHERE (address = $3);")
12
13 echo "the selected building $3 has idbuilding $idbuilding"
14 #mysql -h 127.0.0.1 -P 3306 -u root -padmin -se "USE intro_ict; SELECT * FROM buildings;"
15
16
我想使用bash脚本将数据从mysql数据库显示到控制台。我用不同的引语研究了这个问题的其他解决方案,`但没有任何效果。我正在使用ubuntu for windows,我可以在mysqlworkbench中看到buildings.address列肯定在那里
我不知道该试什么了。有问题的条款:
WHERE address = ZP07
表示查找一个名为address的列,该列等于另一个名为ZP07的列
我猜:
ZP07实际上是一个存储在地址列中的值,并且。。。
表中没有名为ZP07的列。。。
解释错误消息:未知列“ZP07”
要解决此问题,您需要在ZP07值周围加上单引号,例如:
WHERE address = 'ZP07'
这可以通过在shell脚本中添加3美元左右的单引号来实现,例如:
sql1="USE intro_ict; SELECT idbuildings from buildings WHERE address = '$3';"
# and
idbuilding=$(mysql ... "USE intro_ict; SELECT idBuildings FROM Buildings WHERE (address = '$3');")
注意:OP可能需要重新考虑重写idbuilding=$mysql。。。部分使用sql1变量,这样SELECT语句就不必在2个不同的位置进行维护,例如:
idbuilding=$(mysql -h 127.0.0.1 -P 3306 -u root -padmin -se "${sql1}")
请从代码中删除行号。