1

I'm having issues with an indent error in my code. It looks correct... can anyone point out what I'm doing wrong? I keep getting the error on the line of my query.

def invoice_details(myDeliveryID):

    conn = pymssql.connect(myMSSQLserver, myMSSQLuser, myMSSQLpassword, myMSSQLdatabase)
    cursor1 = conn.cursor()

    cursor1.execute('''My Query''' +  "'" + myDeliveryID + "'" + ''' More of my query...''')

    InvoiceDetails = cursor1.fetchone()

    myLocation = "%s" % (InvoiceDetails[0])
    myDate = "%s" % (InvoiceDetails[1])
    myInvoiceNumber = "%s" % (InvoiceDetails[2])
    myAccountNumber = "%s" % (InvoiceDetails[3])

    return myLocation
    return myDate
    return myInvoiceNumber
    return myAccountNumber

    conn.close()
Vong Ly
  • 187
  • 1
  • 8
  • 1
    You can't return 4 things. I think you meant to return a 4-tuple: `return myLocation,myDate,myInvoiceNumber,myAccountNumber` – pushkin Dec 27 '15 at 01:38
  • There is no indentation error in the shown part of the code – Tamas Hegedus Dec 27 '15 at 01:42
  • Okay, I didn't realize you can only have one return. I still get the same error when returning InvoiceDetails and placing after conn.close() – Vong Ly Dec 27 '15 at 01:50
  • You probably want to put your `conn.close()` statement inside your function (i.e. before the return statement). A return statement results in the code leaving the function and returning to the line from which it was called – user1245262 Dec 27 '15 at 01:52

1 Answers1

1

You cannot have multiple return statements in a function.

Instead, you probably meant to return InvoiceDetails (which is a tuple):

def invoice_details(myDeliveryID):
    conn = pymssql.connect(myMSSQLserver, myMSSQLuser, myMSSQLpassword, myMSSQLdatabase)
    cursor1 = conn.cursor()

    cursor1.execute('''My Query''' +  "'" + myDeliveryID + "'" + ''' More of my query...''')

    InvoiceDetails = cursor1.fetchone()

    conn.close()

    return InvoiceDetails

Or, you can make a namedtuple() to also have attribute lookups in addition to positional:

import collections

invoice = collections.namedtuple('Invoice', ['location', 'date', 'number', 'account_number'])
return invoice(*InvoiceDetails)

Also see:

Community
  • 1
  • 1
alecxe
  • 414,977
  • 106
  • 935
  • 1,083