0

I am trying to return 4 up to 100 rows from ladon/python but it is only returning the first row in the for loop. Currently it returns

EDIT: The For loop works if I just do

    **for row in cursor:
            RSOID = row.RSO_ID
            ALIAS=row.ALIAS
            Qty=row.QTY
            print(RSOID)
            print(ALIAS)
            print(Qty)**

ItemNum-1234-ItemNum

Quant-1-Quant

RSOID-1-RSOID

Here is my code:

class OrderLookUpResponse(LadonType):
RSOID = str
ItemNum = str
Quant = str

@ladonize(str,rtype=OrderLookUpResponse)
def LookupOrder(self,OrderID):
    cursor.execute("SELECT ALIAS,QTY,RSO_ID FROM RSK_DETAIL WHERE RSO_ID IN(SELECT RSO_ID FROM RSK_ORDER WHERE ACCT_CODE = 'SCRUBBED')")
    rows=cursor.fetchall()
    for row in rows:
            RSOID = row.RSO_ID
            ALIAS=row.ALIAS
            Qty=row.QTY
            result = OrderLookUpResponse()
            result.RSOID=RSOID
            result.ItemNum=ALIAS
            result.Quant=Qty
            return result
cambria
  • 49
  • 6

2 Answers2

1

You have the return inside the for loop. As a result the function will exit during the first loop and thus you will get only the first result. My advice : remove the extra tabs before the command "return".

You should consider appending to a list all the results:

from ladon.ladonizer import ladonize
from ladon.types.ladontype import LadonType

class OrderLookUpResponse(LadonType):
    RSOID = str
    ItemNum = str
    Quant = str

@ladonize(str,rtype=[OrderLookUpResponse])
def LookupOrder(self,OrderID):
    results=[]
    cursor.execute("SELECT ALIAS,QTY,RSO_ID FROM RSK_DETAIL WHERE RSO_ID IN(SELECT RSO_ID FROM RSK_ORDER WHERE ACCT_CODE = 'SCRUBBED')")
    rows=cursor.fetchall()
    for row in rows:
        RSOID = row.RSO_ID
        ALIAS=row.ALIAS
        Qty=row.QTY
        result = OrderLookUpResponse()
        result.RSOID=RSOID
        result.ItemNum=ALIAS
        result.Quant=Qty
        results.append(result)
    return results

Summary of changes (in order to be easier to follow):
1) added tabs to class 'OrderLookUpResponse' body
2) removed one tab before return.
3) added a list called "results"
4) changed rtype=OrderLookUpResponse to rtype=[OrderLookUpResponse]

Stanislav
  • 2,629
  • 1
  • 29
  • 38
  • Better but now it only returns that last row. – cambria Feb 12 '15 at 19:57
  • Could you try to append all the results as in the example. Does that work? – Stanislav Feb 12 '15 at 20:06
  • Appending does not work. I get a whole new set of errors and mismatched types. – cambria Feb 12 '15 at 20:08
  • I believe is due to the fact that you are specifying that "rtype=OrderLookUpResponse", in other words you are requesting a OrderLookUpResponse, while it is returning a **list** of OrderLookUpResponse. This means that you need to either change the return type or to return 'OrderLookUpResponse'. – Stanislav Feb 12 '15 at 20:48
  • I am not in position to test it so I cannot guarantee it will work however consider my latest version: rtype=[OrderLookUpResponse] – Stanislav Feb 12 '15 at 20:58
0

You may be running into a problem because the for loop is iterating over the value cursor.fetchall(). That is why your code stops after the first line. If you change your rows value into a list, (e.g []) this should help.

Chironian
  • 45
  • 3
  • I changed my rows to the cursor which is list or treated as a list as per the documentation [here](https://code.google.com/p/pyodbc/wiki/Cursor) but no change – cambria Feb 12 '15 at 19:38