Error using Query Parameters with cfscript query

前端 未结 3 1160
后悔当初
后悔当初 2021-01-17 18:27

Here is my code:

var qryStr = \"
            UPDATE templates_email 
            SET title = :title, test_emails = :testEmail, body = :body
            WHERE         


        
相关标签:
3条回答
  • 2021-01-17 18:36

    ColdFusion can get confused when parsing the SQL string to use parameters. The easiest way to solve this shortcoming is to put a space after each of your parameters.

    Since some text editors may remove trailing whitespace, like when saving, I include an empty comment after any space at the end of a line.

    var qryStr = "
        UPDATE templates_email 
        SET title = :title , test_emails = :testEmail , body = :body /**/
        WHERE id = :templateID /**/
    ";
    
    0 讨论(0)
  • 2021-01-17 18:50

    The parser for getting the params doesn't tokenize on return values, only on whitespace (which is really annoying). Try the following:

    var qryStr = "
        UPDATE templates_email 
        SET title = ( :title ), test_emails = ( :testEmail ), body = ( :body )
        WHERE ( id = :templateID )
    ";
    

    The ( and ) should remove any issue with where the parser not being able to recognise where the :params stop and start.

    0 讨论(0)
  • 2021-01-17 18:53

    This error occurs because of the tab and line break characters found in your SQL statement. I normally run below function on my SQL statement to remove these characters.

    string function cleanSQL(required string sqlStatement)
        output="false"
    {
    return trim(reReplace(arguments.sqlStatement, "\t|\n", " ", "all"));
    }
    

    So, your setSQL() can look like:

    q.setSQL(cleanSQL(qryStr))
    

    or simply:

    q.setSQL(reReplace(qryStr, "\t|\n", " ", "all"))
    
    0 讨论(0)
提交回复
热议问题