Как выбрать данные типа 100 без [(100),] из postgresql, используя python? - PullRequest
2 голосов
/ 02 апреля 2020

У меня есть таблица, которая имеет только 1 строку, мне нужно только обновить данные в первой строке. Мне нужно предварительно просмотреть данные при запуске, такие как Total Wealth: - 2000 Ca sh: - 0 Экономия: - 2000. Но это превью, как показано ниже. введите описание изображения здесь Поэтому, пожалуйста, дайте мне решение для этого.

from tkinter import *
import psycopg2

try:
    connection = psycopg2.connect(user="postgres",
                              password="postgres",
                              host="localhost",
                              port="5432",
                              database="money_db")
    cursor = connection.cursor()

    query_cash = "select cash from main where index = 1;"
    query_savings = "select savings from main where index = 1;"

    cursor.execute(query_cash)
    cash = cursor.fetchall()


    cursor.execute(query_savings)
    savings = cursor.fetchall()


    t_w = (cash + savings)

    cursor.execute("commit;")
    if (connection):
        cursor.close()
        connection.close()
        print("PostgreSQL connection is closed")
except (Exception, psycopg2.Error) as error:
    print("Error while fetching data from PostgreSQL", error)

root = Tk()
root.geometry("900x500")
root.resizable(width=False, height=False)

t_w_lbl = Label(text="Total Wealth = " + str(t_w), font=("Calibry", 14), bg="white")
t_w_lbl.place(rely=0.04, relx=0.1)

cash_lbl = Label(text="Cash = " + str(cash), font=("Calibry", 14), bg="white")
cash_lbl.place(rely=0.04, relx=0.45)

photo = PhotoImage(file = "exchange.png")
exchange_btn = Button(image = photo, command = lambda: exchange())
exchange_btn.place(relx = 0.64, rely = 0.03, height = 35, width = 50)

savings_lbl = Label(text="Savings = " + str(savings), font=("Calibry", 14), bg="white")
savings_lbl.place(rely=0.04, relx=0.8)`

1 Ответ

2 голосов
/ 02 апреля 2020

Вместо этого следует использовать метод fetchone и распаковать возвращаемый кортеж (обратите внимание на добавленную запятую).

Изменить:

cash = cursor.fetchall()
...
savings = cursor.fetchall()

на:

cash, = cursor.fetchone()
...
savings, = cursor.fetchone()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...