pg.ddx.io pgsql-general@postgresql.org mailing list archivehelp / color / mirror / Atom feed
Searching Postgresql Database with psycopg2 3+ messages / 3 participants [nested] [flat]
* Searching Postgresql Database with psycopg2 @ 2018-02-01 20:50 brandon wallace <nodnarb@gmx.us> 0 siblings, 2 replies; 3+ messages in thread From: brandon wallace @ 2018-02-01 20:50 UTC (permalink / raw) To: pgsql-general <html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>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.</div> <div>All my other functions to update, add, delete, view all rows are working fine. I am able to run queries on the database and only pull up the rows I need using psql command line no problem.</div> <div> </div> <div> <div>Here is my function.</div> <div> </div> <div> <div>from tkinter import *<br/> import psycopg2</div> <div> </div> <div>def search(title=None, isbn=None, year=0):<br/> '''Search the database rows'''<br/> conn = psycopg2.connect("dbname='books'")<br/> cur = conn.cursor()<br/> cur.execute("SELECT books.title, books.isbn, books.year FROM books WHERE books.title = title OR books.isbn = books.isbn OR books.year = year")<br/> rows = cur.fetchone()<br/> conn.close()<br/> return rows</div> <div> </div> <div> </div> <div> </div> </div> </div></div></body></html> ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Searching Postgresql Database with psycopg2 @ 2018-02-01 20:52 Rob Sargent <robjsargent@gmail.com> parent: brandon wallace <nodnarb@gmx.us> 1 sibling, 0 replies; 3+ messages in thread From: Rob Sargent @ 2018-02-01 20:52 UTC (permalink / raw) To: brandon wallace <nodnarb@gmx.us>; +Cc: pgsql-general > On Feb 1, 2018, at 1: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. > All my other functions to update, add, delete, view all rows are working fine. I am able to run queries on the database and only pull up the rows I need using psql command line no problem. > > > Here is my function. > > from tkinter import * > import psycopg2 > > 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() The “fetchone()” proably has a lot to do with it > conn.close() > return rows > > > ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Searching Postgresql Database with psycopg2 @ 2018-02-01 20:59 David Kohn <djk447@gmail.com> parent: brandon wallace <nodnarb@gmx.us> 1 sibling, 0 replies; 3+ messages in thread From: David Kohn @ 2018-02-01 20:59 UTC (permalink / raw) To: brandon wallace <nodnarb@gmx.us>; +Cc: 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, D ^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-02-01 20:59 UTC | newest] Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2018-02-01 20:50 Searching Postgresql Database with psycopg2 brandon wallace <nodnarb@gmx.us> 2018-02-01 20:52 ` Rob Sargent <robjsargent@gmail.com> 2018-02-01 20:59 ` David Kohn <djk447@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox