0

databaseScrnshotThis is part of my code that should return info from database. Query is not returning anything—not even an error. My code is as follows from my application.py. I managed to get my website on book review to load, authenticate, and make a sign in and signout. Now the important part of returning book queries doesn't seem to call anything to the screen. I am suspecting it could be my submit button, but I have not been lucky with picking out the cause.

@app.route('/search', methods=['GET', 'POST'])
def search():    
    if request.method == "POST":
        title = request.form['byTitle']
        author = request.form['byAuthor']
        year = request.form['byYear']
        isbn = request.form['byIsbn']

        list = []
        text = None
        baseUrl = request.base_url
        if title:
            result = db.execute(" SELECT * FROM books WHERE title LIKE '%"+title+"%' ;").fetchall()
            text = title
        elif author:
            result = db.execute(" SELECT * FROM books WHERE author LIKE '%"+author+"%' ;").fetchall()
            text = author
        elif year:
            result = db.execute(" SELECT * FROM books WHERE year = :year", {'year':year}).fetchall()
            text = year
        else:
            result = db.execute(" SELECT * FROM books WHERE isbn LIKE '%"+isbn+"%' ;").fetchall()
            text = isbn
        #save it in list 
        if result: 
            for i in result : 
                list.append(i)
            itemsCount = len(list)
            return render_template('search.html', baseUrl = baseUrl,  items = list, msg = "Search result found", text = text , itemsCount = itemsCount)

        #Not found message
        else:
            return render_template('search.html', msgNo = "No books found" , text = text)

More info from top of my application.py

from flask import Flask, render_template, redirect, url_for, jsonify, request
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm 
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, Length
from flask_sqlalchemy  import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import Users, requests, psycopg2


app = Flask(__name__)
app.config['SECRET_KEY'] = 'iaddedit'
app.config['SQLALCHEMY_DATABASE_URI'] = 'this is added too'
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)

here's some code from 'search.html'

 <div class="col-sm-12 col-md-9">
            {% if itemsCount %}
            <div
              class="text-center alert alert-success alert-dismissible fade show"
              role="alert"
            >
              <button
                type="button"
                class="close"
                data-dismiss="alert"
                aria-label="Close"
              >
                <span aria-hidden="true">&times;</span>
              </button>
              <p>
               Total <strong> {{itemsCount}} </strong> result found for:
                <strong> {{text}}</strong>
              </p>
            </div>

            <script>
              $(".alert").alert();
            </script>
            {%endif%} {% if msgNo %}
            <div
              class="text-center alert alert-danger alert-dismissible fade show"
              role="alert"
            >
              <button
                type="button"
                class="close"
                data-dismiss="alert"
                aria-label="Close"
              >
                <span aria-hidden="true">&times;</span>
              </button>
              <p>{{msgNo}} for: <strong> {{text}}</strong></p>
            </div>

            <script>
              $(".alert").alert();
            </script>

            {{hello}} {%endif%} {%if items%}
            <table class="table table-hover">
              <thead>
                <tr>
                  <th>Title</th>
                  <th>Author</th>
                  <th>Year</th>
                  <th>ISBN</th>
                </tr>
              </thead>
              <tbody>
                {% for i in items %}
                <tr>
                  <td><a href="{{baseUrl}}/{{i[1]}}"> {{i[2]}}</a></td>
                  <td>{{i[3]}}</td>
                  <td>{{i[4]}}</td>
                  <td><a href="{{baseUrl}}/api/{{i[1]}}"> {{i[1]}}</a></td>
                </tr>
                {% endfor %}
              </tbody>
            </table>
            {%endif %}
          </div>
        </div>

      {%endblock%}
Buhle
  • 1
  • 2
  • 1
    2 things to note: LIKE is *case sensitive*, so that can cause "unexpected" results, and you should not use any form of string formatting to add arguments to queries. Instead use you driver's placeholders and pass the arguments to the driver. – Ilja Everilä Jun 02 '20 at 14:10
  • Thanks @IljaEverilä, but no joy as yet. – Buhle Jun 02 '20 at 16:38
  • I noticed that you're actually doing the right thing for year. You should pass the others like that as well, as shown here https://stackoverflow.com/questions/57120430/how-to-implement-a-search-function-using-a-raw-sql-query, and here https://stackoverflow.com/questions/51463912/cs50-like-operator-variable-substitution-with-expansion – Ilja Everilä Jun 02 '20 at 16:54
  • Just tried that, still nothing...If only it was showing errors... – Buhle Jun 02 '20 at 17:11
  • Likely the data is not as expected, or the search term. Did you make sure that case sensitivity is not the issue? Try replacing LIKE with ILIKE. – Ilja Everilä Jun 02 '20 at 17:18
  • I did, sensitivity seems to be fine... ILIKE not yielding either – Buhle Jun 02 '20 at 17:56
  • It seems it'd benefit your question greatly if you'd provide some sample data and the table schema, and inputs in a usable format. – Ilja Everilä Jun 03 '20 at 05:47

0 Answers0