ID="VC002538"
mysql -u xxxxx --password=xxxxx -e "SELECT orderid FROM Pran_order_detail WHERE orderid='$ID'"
样品
上面的查询我将在Linux shell提示符下执行,它已成功执行以下输出
+------+
| id |
+------+
| 1140 |
+------+
但是当使用os.system()方法在python中做同样的事情时 我没有得到任何输出
ID="VC002538"
os.system('mysql -u xxxxx --password=xxxxx -e "SELECT orderid FROM Pran_order_detail WHERE orderid='ID'" sample')
但是当我尝试直接使用ID值时
os.system('mysql -u xxxxx --password=xxxxx -e "SELECT orderid FROM Pran_order_detail WHERE orderid='\VC002538\'" sample')
这是有效的
请告诉我如何在sql查询中使用os.system()
中的变量
答案 0 :(得分:0)
在你的第一个例子中,Mysql看到的是这个查询:
SELECT orderid FROM Pran_order_detail WHERE orderid='ID'
您需要使用变量ID
构造一个字符串,例如:
"SELECT orderid FROM Pran_order_detail WHERE orderid='%s'" % ID
或
"SELECT orderid FROM Pran_order_detail WHERE orderid='" + ID + "'"
或
"SELECT orderid FROM Pran_order_detail WHERE orderid='{}'".format(ID)
您可以通过Google搜索" python字符串格式化来找到更多信息"。