You are not logged in Log in Join
You are here: Home » Members » Chui Tey » How to access the field names and fields of any arbitrary ZSQL query

Log in
Name

Password

 

How to access the field names and fields of any arbitrary ZSQL query

Introduction

ZSQL is a powerful method of accessing information from relational databases in a platform-independent manner.

Sometimes, it is necessary to access the field names and the values of an SQL Query which cannot be told in advance.

The names attribute

Field names of a ZSQL method can be accessed using the .names attribute

Assumptions

You already have a sql method called SQL

Code:

   <dtml-in "SQL().names()">      
     <dtml-let vname=sequence-item
               vvalue="SQL()[0][_['sequence-index']]">
        <dtml-var vname>,
        <dtml-var vvalue><br>    
     </dtml-let>
   </dtml-in>

 or::

   <dtml-with SQL>
     <dtml-in names>      
     <dtml-let vname=sequence-item
               vvalue="SQL()[0][_['sequence-index']]">
        <dtml-var vname>,
        <dtml-var vvalue><br>    
     </dtml-let>
     </dtml-in>
   </dtml-with>

Comment

The SQL()[0] returns the first row of a SQL query. In your code, you should check that rows exists before trying to access field values.