【发布时间】:2018-12-23 23:10:25
【问题描述】:
我在我的 django 代码中使用 html 的输出,如果它为 null,我希望 mysql 查询忽略 where 子句,如果它有值,则在查询中使用。
下面是我的html:
<form method="post" novalidate>
{% csrf_token %}
<input type="text" name="param1" placeholder="param1">
<input type="text" name="param2" placeholder="param">
<input type="text" name="param3" placeholder="param3">
<input type="submit" name="fetchdata" value="Fetch Data">
<br>
</form>
如何在 MySQL 中实现:
def current_cfg(request):
if request.POST.get("fetchdata"):
param1=request.POST["param1"]
param2=request.POST["param2"] <----aaaa from html as input
param3=request.POST["param3"]
else:
RNC='RRRNCH02'
MO='CORRMALGOSWITCH'
parameterid='DRASWITCH_DRA_VOICE_SAVE_CE_SWITCH'
conn=db.connect(host='localhost', user='update_db', passwd='1qaz!QAZ', db='rnc_parameters')c=conn.cursor()
c=conn.cursor()
c.execute("""select * from rnc_parameters.current_para where (if param1 is null ignore it else param1='aaaaa')""")
current_stats=c.fetchall()
conn.close()
current_stats=list(current_stats)
return render(request,'current.html',{'current_stats':current_stats, 'MO':MO})
你能帮我正确定义下面的部分吗:
c.execute("""select * from rnc_parameters.current_para where (if param1 is null ignore it else param1='aaaaa')""")
【问题讨论】:
-
ignore 是什么意思?在结果中包含它,还是在结果中排除它?顺便说一句,您没有使用 ORM 的原因是什么?
-
@WillemVanOnsem 我的意思是从查询中排除
-
但是你的意思是如果值(所以这里
'aaa'是None)? -
@WillemVanOnsem 假设它可以不是“aaaa”。我想要写下一个查询,如果 param1 为 None,则从 where 子句中排除,如果它是 'aaaa',则包含在查询中
-
@WillemVanOnsem 我没有使用 ORM bcz 我使用的是外部数据库,我的 django 数据库是 sqlite3,我正在从 mysql 数据库获取数据