I am running Python3.5 and Psycopg2 2.6 in a tkinter window. I have this function in a file called backend.py
import psycopg2
def search(year=0, month=0):
'''Search the database for entries.'''
conn = psycopg2.connect("dbname='birth_years'")
cur = conn.cursor()
cur.execute("SELECT * FROM birthdays WHERE year=year OR month=month;")
row = cur.fetchone()
conn.close()
return row
I have frontend.py with this code.
from tkinter import *
import backend
def search_command(year, month):
'''Run queries on the database.'''
listbox1.delete(0, END)
row = backend.search(year_text.get(), month_text.get())
listbox1.insert(END, row)
window = Tk()
window.wm_title("Birthdays")
window.resizable(width=False, height=False)
Grid.rowconfigure(window, 0, weight=1)
Grid.columnconfigure(window, 0, weight=1)
b1 = Button(window, text="Search", width=15, command=search_command)
b1.grid(row=2, column=5)
window.mainloop()
Database:
id year month
1 1999 2
2 2005 5
3 1987 11
4 1988 12
5 1978 10
The postgresql database contains this data. PGAdmin2 is able to run the query fine and select only the row that I want. When I run a query for the year 1988 using the function all I get in the first row of the database.
1 1999 2
row = cur.fetchone()and notfetchall(). You explicitly ask it to only return one value.