SQL PYMYSQL 搜索中的通配符
SQL wildcards in PYMYSQL search
我正在努力为我的 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>
python路线
@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”的结构有关,因为使用“=”语句时查询工作正常。
提前感谢您的帮助。
经过一番尝试,我找到了解决这个问题的方法:
'%' 通配符需要按如下方式放置:
MLN = "%s" % '%' + material_namex + '%'
其余代码正确
进行如下更改:
MLN = "%{0}%".format(material_namex)
并执行:
query = "SELECT id, material, cas, category FROM wegomats WHERE material LIKE '{0}' ORDER BY cas".format(MLN)
其余保持不变。
希望这对你有用。
我正在努力为我的 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>
python路线
@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”的结构有关,因为使用“=”语句时查询工作正常。
提前感谢您的帮助。
经过一番尝试,我找到了解决这个问题的方法: '%' 通配符需要按如下方式放置:
MLN = "%s" % '%' + material_namex + '%'
其余代码正确
进行如下更改:
MLN = "%{0}%".format(material_namex)
并执行:
query = "SELECT id, material, cas, category FROM wegomats WHERE material LIKE '{0}' ORDER BY cas".format(MLN)
其余保持不变。 希望这对你有用。