ColdFusion: Multiple SQL statements in a query?

爱⌒轻易说出口 提交于 2019-12-23 08:54:48

问题


Apparently ColdFusion doesn't like multiple SQL statements within a single query... so what once was this:

SET @sender_user_id = 3, @recipient_user_id = 5;

INSERT INTO messages (message_type, sender_id, message_title, message_content) 
  VALUES(3, @sender_user_id, 'One more thing...', 'I am testing this message');

SET @saved_message_id = LAST_INSERT_ID();

INSERT INTO message_recipient (message_id, user_id) 
  VALUES(@saved_message_id, @recipient_user_id);

INSERT INTO message_status (message_id, user_id, is_read, read_datetime, is_deleted, deleted_datetime)
  VALUES (@saved_message_id, @recipient_user_id, 0, NULL, 0, NULL);

Get's turned into this:

<cftransaction>

    <cfquery name="insertMessage" dataSource="mySource">

        SET @sender_user_id = 3, @recipient_user_id = 5;

    </cfquery>
    <cfquery name="insertMessage2" dataSource="mySource">

        INSERT INTO messages (message_type, sender_id, message_title, message_content) 
        VALUES(3, @sender_user_id, '#params.message_title#', '#params.message_content#');

    </cfquery>
    <cfquery name="insertMessage3" dataSource="mySource">

        SET @saved_message_id = LAST_INSERT_ID();

    </cfquery>
    <cfquery name="insertMessage4" dataSource="mySource">   

        INSERT INTO message_recipient (message_id, user_id) 
        VALUES(@saved_message_id, @recipient_user_id);

    </cfquery>
    <cfquery name="insertMessage5" dataSource="mySource">   

        INSERT INTO message_status (message_id, user_id, is_read, read_datetime, is_deleted, deleted_datetime)
        VALUES (@saved_message_id, @recipient_user_id, 0, NULL, 0, NULL);

    </cfquery>

</cftransaction>

This works... but I'm wondering if there is some ColdFusion thing that I don't know about. Otherwise, this works.


回答1:


In ColdFusion Admin, go to your Data Source definition form and add "allowMultiQueries=true" to the Connection String box. Once you do that, you can pass multiple semi-colon separated queries in a single CFQUERY tag. Just make sure you're using CFQUERYPARAM to screen for SQL Injection Attacks.




回答2:


In case anyone is finding this and putting the allowMultiQueries=true in the connection box is not working, I finally tried appending it to the actual JDBC url like this jdbc:mysql://127.0.0.1:3306/mydbname?allowMultiQueries=true. It worked on the first try afterwards.



来源:https://stackoverflow.com/questions/6299326/coldfusion-multiple-sql-statements-in-a-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!