Search code examples
pythonvariable-assignmentconditional-operator

how to assign variable value with condition


I am work with python. I have code like this :

def return_auth_notificatio():
    shiporderid = all_Orderid
    cursor.execute("select *from return_auth_notification")
    email_record = cursor.fetchall()
    for row in email_record:
        Orderid = row[1]
        Customercomment =row[4]
        Amountreturn =row[5]
        condition_return_auth_notificatio=(Customercomment,Amountreturn if shiporderid == Orderid else None)
        assign_return_auth_notificatio=(condition_return_auth_notificatio if !Null )
    return(assign_return_auth_notificatio)

data=return_auth_notificatio()
all_Customercomment=data[0]
all_Amountreurn=data[1]

I want this variable Customercomment,Amountreturn if records whose Orderid matches all_Orderid


Solution

  • Put the condition in the query instead of the loop.

    def return_auth_notificatio():
        customer_comments = []
        amount_returns = []
        cursor.execute("""
            select Customercomment, Amountreturn 
            from return_auth_notification 
            WHERE Orderid = %s""", (all_Orderid,))
        for comment, amount in cursor.fetchall():
            customer_comments.append(comment)
            amount_returns.append(amount)
        return customer_comments, amount_returns
    
    all_Customercomment, all_Amountreturn = return_auth_notificatio()
    

    The placeholder %s assumes you're using MySQL. If it's SQLite use ? instead.