有个商品表,我打算通过品名查询,但考虑下这种商品名很乱,势必要用正则来取:
+----+---------------------------------------+
| id | name |
+----+---------------------------------------+
| 1 | r510vc 15.6 英寸笔记本 |
| 2 | y400n 14.0 英寸笔记本电脑 |
| 3 | g150th 15.6 英寸游戏本 |
| 4 | x550cc 15.6 英寸笔记本 |
| 5 | x240 超级本 |
| 6 | u330p 13.3 英寸超级本 |
| 7 | svp13226scb 触控超级本 |
| 8 | ipad mini 7.9 英寸平板电脑 |
| 9 | ipad air 9.7 英寸平板电脑 |
| 10 | ipad mini 配备 retina 显示屏 |
| 11 | ideacentre c340 20 英寸一体电脑 |
| 12 | vostro 3800-r1206 台式电脑 |
| 13 | imac me086ch/a 21.5 英寸一体电脑 |
| 14 | at7-7414lp 台式电脑 linux ) |
| 15 | z220sff f4f06pa 工作站 |
| 16 | poweredge ii 服务器 |
| 17 | mac pro 专业级台式电脑 |
| 18 | hmz-t3w 头戴显示设备 |
| 19 | 商务双肩背包 |
| 20 | x3250 m4 机架式服务器 |
| 21 | 商务双肩背包 |
+----+---------------------------------------+
比如输入电脑,就显示含有电脑关键字的。于是我写了个查询的类,下面定义一个依据名称查询的方法:
def search_by_name(self):
goods_name = input('请输入您要查询的商品名:')
# sql_command = 'select * from goods where name regexp ".*%s.*"' % goods_name
# self.execute_sql_command(sql_command)
#self.cursor.execute('select * from goods where name regexp ".*%s.*"',[goods_name,])
sql = 'select * from goods where name=%s'
paras = (goods_name,)
self.cursor.execute(sql,paras)
for item in self.cursor.fetchall():
print(item)
1.被注释掉的 self.cursor.execute('select * from goods where name regexp ".*%s.*"',[goods_name,])一句是我想达到的目标,但是发现不成功
于是我猜想可能是正则语句太复杂了?上面有一重引号,导致解析时候发生了错误
2.为了验证呢,于是写了
sql = 'select * from goods where name=%s'
paras = (goods_name,)
self.cursor.execute(sql,paras)
这三行没问题,没有用正则,只是普通参数化查询,是可以成功的。
3.另外非参数化的构造 sql 语句再执行的方法,也能把这个正则查询跑出来,但是会有 sql 注入问题(也就是上面被注释掉的前两行的方法)
因为是 python 初学者,这个问题应该在前端做规则限制吧。但是还是想知道应该怎么写这种带参的正则查询呢?
+----+---------------------------------------+
| id | name |
+----+---------------------------------------+
| 1 | r510vc 15.6 英寸笔记本 |
| 2 | y400n 14.0 英寸笔记本电脑 |
| 3 | g150th 15.6 英寸游戏本 |
| 4 | x550cc 15.6 英寸笔记本 |
| 5 | x240 超级本 |
| 6 | u330p 13.3 英寸超级本 |
| 7 | svp13226scb 触控超级本 |
| 8 | ipad mini 7.9 英寸平板电脑 |
| 9 | ipad air 9.7 英寸平板电脑 |
| 10 | ipad mini 配备 retina 显示屏 |
| 11 | ideacentre c340 20 英寸一体电脑 |
| 12 | vostro 3800-r1206 台式电脑 |
| 13 | imac me086ch/a 21.5 英寸一体电脑 |
| 14 | at7-7414lp 台式电脑 linux ) |
| 15 | z220sff f4f06pa 工作站 |
| 16 | poweredge ii 服务器 |
| 17 | mac pro 专业级台式电脑 |
| 18 | hmz-t3w 头戴显示设备 |
| 19 | 商务双肩背包 |
| 20 | x3250 m4 机架式服务器 |
| 21 | 商务双肩背包 |
+----+---------------------------------------+
比如输入电脑,就显示含有电脑关键字的。于是我写了个查询的类,下面定义一个依据名称查询的方法:
def search_by_name(self):
goods_name = input('请输入您要查询的商品名:')
# sql_command = 'select * from goods where name regexp ".*%s.*"' % goods_name
# self.execute_sql_command(sql_command)
#self.cursor.execute('select * from goods where name regexp ".*%s.*"',[goods_name,])
sql = 'select * from goods where name=%s'
paras = (goods_name,)
self.cursor.execute(sql,paras)
for item in self.cursor.fetchall():
print(item)
1.被注释掉的 self.cursor.execute('select * from goods where name regexp ".*%s.*"',[goods_name,])一句是我想达到的目标,但是发现不成功
于是我猜想可能是正则语句太复杂了?上面有一重引号,导致解析时候发生了错误
2.为了验证呢,于是写了
sql = 'select * from goods where name=%s'
paras = (goods_name,)
self.cursor.execute(sql,paras)
这三行没问题,没有用正则,只是普通参数化查询,是可以成功的。
3.另外非参数化的构造 sql 语句再执行的方法,也能把这个正则查询跑出来,但是会有 sql 注入问题(也就是上面被注释掉的前两行的方法)
因为是 python 初学者,这个问题应该在前端做规则限制吧。但是还是想知道应该怎么写这种带参的正则查询呢?