[Python] Return True/False of EXISTS Postgres query not being recognized by IF statement

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    def track_exists():
    conn = psycopg.connect("dbname=postgres user=postgres password=***)
    cur = conn.cursor()
    sqltest = "SELECT EXISTS(SELECT 1 FROM CUSTOMERS WHERE CUSTOMERS.PHONE = '608-888-8888')"
    cur.execute(sqltest)
    result = cur.fetchall()
    if result == [(True,)]:
    print("working")
    dosomething()
    elif result == "":
    print("not working")


    When running this function in VS CODE the output returns exactly, [(True,)] to the screen . My if statement does not recognize this as being true and returns my "not working" statement. No matter what way I've tried different versions of true, IE "True:" , "(True)," "True," etc...

    phonestring = phone_str
    print (phonestring)
    conn = psycopg.connect("dbname=postgres user=postgres password=***)
    cur = conn.cursor()
    sqltest = "SELECT EXISTS(SELECT 1 FROM CUSTOMERS WHERE CUSTOMERS.PHONE = (%s))"
    sqldata = (phonestring)
    cur.execute(sqltest,sqldata)
    result = cur.fetchone()
    if result == [(True,)][0]:
    print("Customer Exists")
    elif result == [(False,)][0]:
    print("New customer")

    Continue reading...

Compartilhe esta Página