Question
· May 1

Embedded Python query

Hi Guys,

How can I use this same objectscript principle in embedded python to get the value of each field in my loop? 

  s sql="SELECT ID, Name, Age FROM Sample.Person WHERE Age="_Myage    

     Set RS=##class(%ResultSet).%New()

    Set Ret=RS.Prepare(sql)

    Set Ret=RS.Execute()

    While RS.Next()

    {

         Set name=RS.GetData(2)

}

it should be similar to this example which lists the whole row but how can I get the value of each field or if I can use GetData?  

 

Thanks

Product version: IRIS 2024.3
Discussion (2)1
Log in or sign up to continue

code below might help:

import iris

# sql statement
sql = "SELECT ID, Name, Age FROM Sample.Person WHERE Age = ?"
result = iris.sql.exec(sql, Myage)

for row in result:
    ## Unpack tuple directly
    id, name, age = row  
    #Access by index (0-based in this case)
    name = row[1]  # Name is 2nd column (index 1)
    
    # Print values
    print(f"ID: {id}, Name: {name}, Age: {age}")