Re: Searching Postgresql Database with psycopg2 - Mailing list pgsql-general

From David Kohn
Subject Re: Searching Postgresql Database with psycopg2
Date
Msg-id CAJhMaBggLrT91V7LxNt9PrAC29PHmvLpW7WnNTR2u7THx5CN2Q@mail.gmail.com
Whole thread Raw
List pgsql-general
Because you're only getting the first row of the query result, even though the cursor that contains the result (should be server side in this case) does have more rows. 
On Thu, Feb 1, 2018 at 3:50 PM brandon wallace <nodnarb@gmx.us> wrote:
I have created a function to search a Postgresql database but it only pulls up the first row of the database no matter what value I search for. Why would it only pull up the first row of the database? I am using Psycopg2 2.6 and Python 3.5.
...
def search(title=None, isbn=None, year=0):
    '''Search the database rows'''
    conn = psycopg2.connect("dbname='books'")
    cur = conn.cursor()
    cur.execute("SELECT books.title, books.isbn, books.year FROM books WHERE books.title = title OR books.isbn = books.isbn OR books.year = year")
    rows = cur.fetchone()
There's your problem. You'll want to either use fetchall() if you want to get all the rows, see http://initd.org/psycopg/docs/cursor.html#cursor.fetchone  
    conn.close()
    return rows
best,

pgsql-general by date:

Previous
From: Rob Sargent
Date:
Subject: Re: Searching Postgresql Database with psycopg2
Next
From: Andrew Bartley
Date:
Subject: Rolls