0

I made a search system for project 1 that returns the author, title, and year of publication of a book when you give the ISBN. However, my code is returning extra characters. If I input the ISBN 0307279464, I get the output: ('A Walk in the Woods',) by ('Bill Bryson',) Published in (1997,) Why is it outputting extra characters and how can I fix this?

Here is my code for application.py:

def bookpage():
    bookisbn = request.args.get("bookisbn")
    author=db.execute("SELECT author FROM books WHERE isbn = :isbn",
        {"isbn":bookisbn}).fetchone()
    title=db.execute("SELECT title FROM books WHERE isbn = :isbn",
        {"isbn":bookisbn}).fetchone()
    year=db.execute("SELECT year FROM books WHERE isbn = :isbn",
        {"isbn":bookisbn}).fetchone()
    if author != None and title != None and year != None:
        return render_template("bookpage.html", author=author, title=title, year=year)
    else:
        return render_template("error404.html")

Here is the HTML code for the search bar, which is part of a Bootstrap navbar:

      <input class="form-control mr-sm-2" type="search" name="bookisbn" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search by ISBN</button>
    </form>

Here is the HTML code for the book page:


{% block head %}

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
{% endblock %}

{% block body %}
  <h1 text-align=center> {{title}} by {{author}} </h1>
  <h2 text-align=center> Published in {{year}} </h2>

{% endblock %}

My book database does not have any of these extra characters that are being returned. Why am I seeing extra characters and how can I remove them? I have already tried using python replace and translate functions.

1 Answer 1

0

From the enter link description herepython sqlite doc (emphasis added):

fetchone()

Fetches the next row of a query result set, returning a single sequence, or None when no more data is available.

The "sequence" in this case is a tuple. You would get the desired data using the usual python syntax, ie tuple[index].

2
  • Thank you for the answer. How should I fetch data from my database though? Jun 10, 2020 at 15:04
  • info added to answer Jun 11, 2020 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .