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)