【发布时间】:2018-05-15 14:31:45
【问题描述】:
我正在努力为我的 Flask 应用程序组合一个简单的 SQL 通配符搜索。
我正在使用 HTML 表单字段来运行一个简单的 mysql 查询“WHERE CustomerName LIKE 'a%'”-https://www.w3schools.com/sql/sql_wildcards.asp - 但是我无法为搜索占位符获得正确的定位...
这里是 HTML 表单域(工作正常)
form method="GET" action>
<div class="form-group">
<div class="col-sm-3">
<input type="text" placeholder="Material Name" name="Material_Name" action=/search class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="submit" value="SEARCH" class="btn btn-primary btn-block">
</div>
</div>
蟒蛇路线
@app.route('/wegowo')
def wegowo():
material_namex = request.args.get('Material_Name','')
try:
tabledata = DB.wego_wo(material_namex)
except Exception as e:
print(e)
tabledata = None
return render_template('wegowo.html', tabledata=tabledata)
最后是数据库辅助函数
def wego_wo(self,material_namex):
connection = self.connect()
MLN = "%s" % material_namex
try:
query = "SELECT id, material, cas, category FROM wegomats WHERE material LIKE '%s' ORDER BY cas" %(MLN);
with connection.cursor(pymysql.cursors.DictCursor) as cursor:
cursor.execute(query)
return cursor.fetchall()
finally:
connection.cursor
错误似乎与 LIKE '%s' 的结构有关,因为在使用 '=' 语句时查询工作正常。
提前感谢您的帮助。
【问题讨论】:
-
澄清一下,通配符应该是 a 查找以“a”开头的任何值,所以可能有某种方法可以修改 - LIKE '%s' ORDER BY cas" %(MLN); 我的查询的一部分,以允许以搜索开头。我尝试了 %s% 和/或修改 (MLN) 语句之类的方法。