9
votes

My boss wants me to use cfscript instead of tags for database interaction. Does anybody know of any good tutorials? I bought the Adobe ColdFusion application development book, vol 2. But it does not have much on scripting. I did google and found this site, but it did not explain much.

Does any body know of any good tutorials on accessing the data base in CFScript?

Basically I have to convert the following to using CFScript:

<cfquery name="drafts" datasource="ICEchat">
    SELECT * from Messages where IsTemp=1 and LinkA=#FORM.LinkA# and LinkB=#FORM.LinkA#
</cfquery>
<cfif drafts.recordcount GT '0'>
    <cfquery name="Attachments" datasource="ICEchat">
        SELECT * FROM Attachments where id=2
    </cfquery>
    { Message:"<cfoutput query="drafts">#Message#</cfoutput>", Attachments:[<cfoutput query="attachments">
        "#url#"<cfif attachments.currentRow LT attachments.recordcount>,</cfif>
    </cfoutput>]}
<cfelse>
    <cfquery name="addrecord" datasource="ICEchat">
        INSERT INTO Messages 
        VALUES(1,1,' ',1)
    </cfquery>
    { Message:"NA", Attachments:[]}
</cfif>
3
You're right, Jake, that it was pretty awful. But it took me less than a minute to fix it. Which is more constructive: Complaining or fixing it? - Adam Tuttle
Well, here is the official documentation. help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/… - Jason Dean
@AdamTuttle you're right, of course. More virtue to be found in overlooking such things. - Jake Feasel
Hi,thank you for the link, Guess I need to look at my typing more, - Ted pottel
Which version of ColdFusion. The current version, 2016, has queryExecute() which a very clean syntax. - James A Mohler

3 Answers

10
votes

From the 4th link on google for "cfscript query tutorial":

<CFSCRIPT>
    myQry = new Query(); // new query object     
    myQry.setSQL("select bookid, title, genre from app.books where bookid = :bookid"); //set query
    myQry.addParam(name="bookid",value="5",CFSQLTYPE="CF_SQL_INTEGER"); // add query param
    qryRes = myQry.execute(); // execute query
    writedump(qryRes.getResult().recordcount, true); // get resultcount
    writedump(qryRes.getResult(), false); // dump result
    writeoutput('<BR>');
</CFSCRIPT>

That ought to tell you everything you need to know.

Also, you really should not be creating JSON manually, no matter how simple it is. Use serializeJson().

6
votes

Didn't test this, but this should do it.

<cfscript>
   local.drafts = new Query();
   local.drafts.setDatasource("ICEchat");
   local.drafts.addParam(name="linkA", value="#form.linkA#", cfsqltype="CF_SQL_VARCHAR");
   local.drafts.addParam(name="linkB", value="#form.linkB#", cfsqltype="CF_SQL_VARCHAR");
   local.drafts.setSQL("SELECT * from Messages where IsTemp=1 and LinkA = :linkA and LinkB = :linkA");
   local.drafts.execute().getResult();

   if (local.drafts.recordcount GT 0) {
      local.attachments = new Query();
      local.attachments.setDatasource("ICEchat");
      local.attachments.setSQL("SELECT * FROM Attachments where id=2");
      local.attachments.execute().getResult();
      WriteOutput("{ Message: ");
      for (i=1; i LTE local.drafts.recordcount; i=i+1) {
         WriteOutput(local.drafts.message[i]);
      }
      WriteOutput(", Attachments: ");
      for (i=1; i LTE local.attachments.recordcount; i=i+1) {
         WriteOutput(local.drafts.url[i]);
         if (i LT local.attachments.recordcount) {
            WriteOutput(", ");
         }
      }
      WriteOutput("}");
   } else {
      local.q = new Query();
      local.q.setDatasource("ICEchat");
      local.q.setSQL("INSERT INTO Messages VALUES(1,1,' ',1)");
      local.q.execute();
      WriteOutput("{ Message:"NA", Attachments:[]}");
   }
</cfscript>
-3
votes

I have been searching for a solution for the same error. I get not defined errors or construct errors. Have been chatting with Ray Camden for the past day, but everything he has suggested is not doing what I need either. I have been working on a website conversion from the standard CF tags to cfscript.

Ray suggested that the .execute(); is what pulls the recordcount and suggested this is all was needed: x=queryExecute(); would fetch the recordcount, with (x) being the query. He suggested not to use getPrefix(); but I read that the .getPrefix(); is what pulls the recordcount. He is experienced I am sure you know Ray Camden, but I keep getting the same error no matter what I try doing inside of my code.