Programmatically creating a query object in ColdFusion

Readability

Programmatically creating a query object in ColdFusion

The fol­low­ing exam­ple shows how you can pro­gram­mat­i­cally cre­ate a query object using the queryNew(), queryAddRow(), and querySetCell() meth­ods in ColdFusion.


    users = queryNew("firstName,lastName");
    queryAddRow(users, 9);
    
    querySetCell(users, "firstName", "Lauren", 1);
    querySetCell(users, "lastName", "P.", 1);
    
    querySetCell(users, "firstName", "Joan", 2);
    querySetCell(users, "lastName", "L.", 2);
    
    querySetCell(users, "firstName", "Peter", 3);
    querySetCell(users, "lastName", "d.", 3);
    
    querySetCell(users, "firstName", "Brian", 4);
    querySetCell(users, "lastName", "O.", 4);
    
    querySetCell(users, "firstName", "Jacob", 5);
    querySetCell(users, "lastName", "G.", 5);
    	
    querySetCell(users, "firstName", "Ella", 6);
    querySetCell(users, "lastName", "M.", 6);
    
    querySetCell(users, "firstName", "Joann", 7);
    querySetCell(users, "lastName", "A.", 7);
    
    querySetCell(users, "firstName", "Robert", 8);
    querySetCell(users, "lastName", "V.", 8);
    
    querySetCell(users, "firstName", "Steven", 9);
    querySetCell(users, "lastName", "S.", 9);
    
    writeDump(users);

And the out­put of the <CFSCRIPT> tag is as fol­lows:
QueryNew()

The following example shows how you can programmatically create a query object using the queryNew(), queryAddRow(), and querySetCell() methods in ColdFusion.

<cfscript>
    users = queryNew("firstName,lastName");
    queryAddRow(users, 9);
 
    querySetCell(users, "firstName", "Lauren", 1);
    querySetCell(users, "lastName", "P.", 1);
 
    querySetCell(users, "firstName", "Joan", 2);
    querySetCell(users, "lastName", "L.", 2);
 
    querySetCell(users, "firstName", "Peter", 3);
    querySetCell(users, "lastName", "d.", 3);
 
    querySetCell(users, "firstName", "Brian", 4);
    querySetCell(users, "lastName", "O.", 4);
 
    querySetCell(users, "firstName", "Jacob", 5);
    querySetCell(users, "lastName", "G.", 5);
 
    querySetCell(users, "firstName", "Ella", 6);
    querySetCell(users, "lastName", "M.", 6);
 
    querySetCell(users, "firstName", "Joann", 7);
    querySetCell(users, "lastName", "A.", 7);
 
    querySetCell(users, "firstName", "Robert", 8);
    querySetCell(users, "lastName", "V.", 8);
 
    querySetCell(users, "firstName", "Steven", 9);
    querySetCell(users, "lastName", "S.", 9);
 
    writeDump(users);
</cfscript>

And the output of the <CFSCRIPT> tag is as follows:
QueryNew()

Leave a Reply

Your email address will not be published.