Python函数IndentationError:意外缩进

时间:2015-12-27 01:37:00

标签: python sql-server indentation

我的代码中出现了缩进错误。它看起来是正确的...任何人都可以指出我做错了什么?我一直在查询中得到错误。

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()

1 个答案:

答案 0 :(得分:1)

您不能在函数中包含多个return语句。

相反,你可能想要返回InvoiceDetails(这是一个元组):

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

或者,您可以使namedtuple()除了位置之外还具有属性查找:

import collections

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

另见: