2017-12-01 9 views
0

나는 Flask 응용 프로그램에 대해 간단한 SQL 와일드 카드 검색을하기 위해 고심하고 있습니다.PYMYSQL 검색에서 SQL 와일드 카드

나는 간단한 MySQL의 쿼리를 실행하는 HTML 양식 필드를 사용하고 "WHERE CUSTOMERNAME LIKE '는 %'"- 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) 
에 '='문을 사용할 때 쿼리가 잘 작동하기 때문에 16,

마지막으로 데이터베이스 도우미 기능

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'이 (가)의 구조 주위 될 것으로 보인다.

도움을 주셔서 감사합니다.

+0

그냥 명확히

MLN = "%s" % '%' + material_namex + '%' 

나머지 코드가 정확한지를 와일드 카드는 "a"로 시작하는 모든 값을 찾습니다. 따라서 LIKE '% s'을 (를) 수정할 수있는 방법이있을 수 있습니다. ORDER BY cas "% (MLN); 허용 할 쿼리의 일부 검색 시작. % s % 및/또는 (MLN) 문 수정 등을 시도했습니다. –

답변

0

후 꽤 나는이 문제에 대한 해결 세례반 시도 : 사업자로 배치해야하는 '%'와일드 카드는 다음과 같습니다