Contact Us

Let us know to help you out quickly!

 Bharat Patel     Feb,08 2017

cfquery Using cfscript

We can use cfscript to execute a query passing SQL statements to a data source. We can execute sql statement using execute method with query object. Look below for explaination and details. I hope it is helpful for you.

How to define query object in cfscript:

<cfscript>queryObj = new Query();<!--- OR --->queryObj = createObject("component","query");</cfscript>

Properties :

name blockfactor cachedafter
cachedwithin dataSource dbtype
debug maxRows password
result timeout username
sql

All attributes are supported by cfquery tag can be used as attribute.

Example:

<cfquery name="myList"></cfquery>can be used asqueryObj.setName("myList");

Methods:

addparam (Add Parameter of query).

queryObj.addParam(name="mediaId",value="1",cfsqltype="numeric");

execute (for executing sql statements);

result = queryObj.execute(sql="select ArtName from art where mediaId = :mediaId");

clearParams (To remover all set param with statements.)

queryObj.clearParams();

How to set properties and method with execute sql statements.

<cfscript>queryObj = new query();queryObj.setDatasource("cfartgallery");queryObj.setName("qListOfArts");queryObj.addParam(name="price",value="32000",cfsqltype="NUMERIC");queryObj.addParam(name="mediaid",value="1",cfsqltype="NUMERIC");queryObj.addParam(name="isSold",value="0",cfsqltype="SMALLINT");result = queryObj.execute(sql="SELECT artname,description,price FROM Art WHERE mediaId = :mediaid and isSold = :isSold and price > :price");qListOfArts = result.getResult();metaInfo = result.getPrefix();queryObj.clearParams();writeDump(qListOfArts);writeDump(metaInfo);</cfscript>

Query output and Meta information.