Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Result Object Methods (See related posts)

// Assuming that we have set result to being a result object we can use the following methods:

len(result)
     this will show the number rows returned (which would be 3 in the example above).

result.names()
    a list of all the column headings, returning a list containing emp_id, first, last and salary

result.tuples()
    returns a list of tuples in our example:

    [(43, 'Bob', 'Roberts', 50000),
     (101, 'Cheeta', 'leCat', 100000),
     (99, 'Jane', 'Junglewoman', 100001)]

result.dictionaries()
    will return a list of dictionaries, with one dictionary for each row:

    [{'emp_id': 42, 'first': 'Bob','last': 'Roberts', 'salary': 50000},
     {'emp_id': 101, 'first: 'Cheeta', 'last': 'leCat', 'salary': 100000},
     {'emp_id': 99, 'first': 'Jane', 'last': 'Junglewoman', 'salary': 100001}]

result.data_dictionary()
    returns a dictionary describing the structure of the results table. 

The dictionary has the key name, type, null and width. 
Name and type are self explanatory, null is true if that field may contain a null value and width is the width in characters of the field. 
Note that null and width may not be set by some Database Adapters.

result.asRDB()
    displays the result in a similar way to a relational database. 

The DTML below displays the result below:
    <pre>
      <dtml-var "list_all_employees().asRDB()">
    </pre>

    ... displays ...

    emp_id first last salary
    42 Bob Roberts 50000
    101 Cheeta leCat 100000
    99 Jane Junglewoman 100001

result[0][1]
    return row 0, column 1 of the result, bob in this example. 

Be careful using this method as changes in the schema will cause unexpected results. 

You need to create an account or log in to post comments to this site.


Click here to browse all 4852 code snippets

Related Posts