2017-04-22 149 views
0

文件models.py瓶卡桑德拉查詢LIKE

class Document(db.Model): 
    __table_name__ = 'document' 

    nim = db.columns.Integer(primary_key=True) 
    prodi = db.columns.Text(primary_key=True) 
    tahun = db.columns.Date(primary_key=True, clustering_order="DESC") 
    title = db.columns.Text(primary_key=True) 
    nama_mhs = db.columns.Text() 
    angkatan = db.columns.Date() 
    intisari = db.columns.Text() 
    kata_kunci = db.columns.Text() 
    pembimbing = db.columns.Text() 
    file_doc = db.columns.Text() 
    password = db.columns.Text() 

    def __repr__(self): 
     return '<Document %r>' % (self.nim, self.nama_mhs, self.angkatan, self.tahun, self.prodi, self.judul, self.kata_kunci, self.intisari, self.pembimbing, self.password, self.file_doc) 

這是我創建的搜索表單,search.html

<from action="{{ url_for('search') }}" method="POST"> 
    <input name="search" type="text" class="form-control" placeholder="Search"> 
    <button type="submit"> 
</from> 

如何進入隨機文本?文件views.py方法搜索()

@app.route('/search', methods=['GET','POST']) 
def search() 
    q1 = sesi.execute(" SELECT title FROM document WHERE title LIKE '%request.form['search']%' ") 
    return render_template("title_search.html", q1=q1) 

title_search.html

{% extends "base.html" %} 
{% block content %} 
{% include "search_document.html" %} 
<hr> 
{% for data in q1 %} 
<div class="col-md-8"> 
    <h3>Title : <a href="#"> {{ data.title }} </a></h3> 
    <hr> 
</div> 
{% endfor %} 
{% endblock %} 

我得到這個在運行

SyntaxException:錯誤從服務器:代碼= 2000 [語法的CQL錯誤 query] message =「line 1:61 missing'EOF at'search'(... document WHERE title LIKE'%request.from ['[search]'] ...)」

回答

1

這是你更新後沒什麼太大的幫助,根本原因是,在你的views.py查詢字符串是無效的,你最好格式化查詢字符串是這樣的:

@app.route('/search', methods=['GET','POST']) 
def search() 
    q1 = sesi.execute("SELECT title FROM document WHERE title LIKE '%{}%'".format(request.form['search'])) 
    return render_template("title_search.html", q1=q1) 

查詢字符串將是一樣,如果你輸入模板字符串「物理」的形式:

"SELECT title FROM document WHERE title LIKE '%Physics%'" 

希望這將解決您的問題。

+0

並未奏效尚未 –

+0

能否請您發佈完整的跟蹤日誌? –

+0

我試圖添加一個跟蹤到我的問題,我希望你能理解結構 –