Searching Postgresql Database with psycopg2

From: "brandon wallace" <nodnarb(at)gmx(dot)us>
To: pgsql-general(at)postgresql(dot)org
Subject: Searching Postgresql Database with psycopg2
Date: 2018-02-01 20:50:37
Message-ID: trinity-629ff67a-1229-498d-88af-8c4019b21414-1517518236981@3c-app-mailcom-bs09
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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>&nbsp;</div>

<div>&nbsp;
<div>Here is my function.</div>

<div>&nbsp;</div>

<div>
<div>from tkinter import *<br/>
import psycopg2</div>

<div>&nbsp;</div>

<div>def search(title=None, isbn=None, year=0):<br/>
&nbsp;&nbsp;&nbsp; &#39;&#39;&#39;Search the database rows&#39;&#39;&#39;<br/>
&nbsp;&nbsp;&nbsp; conn = psycopg2.connect(&quot;dbname=&#39;books&#39;&quot;)<br/>
&nbsp;&nbsp;&nbsp; cur = conn.cursor()<br/>
&nbsp;&nbsp;&nbsp; cur.execute(&quot;SELECT books.title, books.isbn, books.year FROM books WHERE books.title = title OR books.isbn = books.isbn OR books.year = year&quot;)<br/>
&nbsp;&nbsp;&nbsp; rows = cur.fetchone()<br/>
&nbsp;&nbsp;&nbsp; conn.close()<br/>
&nbsp;&nbsp;&nbsp; return rows</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>&nbsp;</div>
</div>
</div></div></body></html>

Attachment Content-Type Size
unknown_filename text/html 1.3 KB

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2018-02-01 20:52:51 Re: Searching Postgresql Database with psycopg2
Previous Message Joshua D. Drake 2018-02-01 19:50:50 What is your psql tip?