如何在coldfusion中使用CFQuery执行2个或多个插入语句?

发布于 2024-08-14 11:11:13 字数 151 浏览 4 评论 0原文

是否可以使用 cfquery 执行 2 条插入或更新语句?

如果是的话怎么办?

如果不是,在 Coldfusion 中执行多个查询的最佳方法是什么(仅打开一个到数据库的连接)。

我想每次我们调用 cfquery 时我们都会打开新的连接数据库

Is it possible to Execute 2 insert or Update Statements using cfquery?

If yes how?

if no, what is the best way to execute multiple queries in Coldfusion, by opening only one Connection to DB.

I think every time we call cfquery we are opening new connection DB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧人 2024-08-21 11:11:13

是否可以执行2次插入或
使用 cfquery 更新语句?

很可能是的。但是否可以运行多个语句取决于您的数据库类型和驱动程序/连接设置。例如,当您创建 MS SQL 数据源时,默认情况下允许 IIRC 多个语句。而 MySQL 驱动程序通常默认情况下禁用多个语句。那是为了帮助避免sql注入。因此,在这种情况下,您必须在连接设置中显式启用多个语句。否则,您不能使用多个语句。还有一些数据库(通常是桌面数据库,如 MS Access)根本不支持多语句。所以我认为这个问题没有一个笼统的答案。

如果两个插入/更新语句相关,那么您绝对应该按照 Sam 的建议使用 cftransaction。这确保了语句被视为一个单元:即它们要么全部成功,要么全部失败。因此,您不会留下部分或不一致的数据。为了实现这一点,事务中的两个查询将使用单个连接。

我想每次我们调用 cfquery 时我们
正在打开新的连接数据库

正如 Sam 提到的,这取决于您的设置以及您是否正在使用 cftransaction。如果启用维护连接(在 CF 管理器中的数据源设置下),CF 将维护一个打开的连接池。因此,当您运行查询时,CF 只是从池中获取一个打开的连接,而不是每次都打开一个新连接。使用 cftransaction 时,所有查询都应使用相同的连接。无论是否启用维护连接。

Is it possible to Execute 2 insert or
Update Statements using cfquery?

Most likely yes. But whether you can run multiple statements is determined by your database type and driver/connection settings. For example, when you create an MS SQL datasource, IIRC multiple statements are allowed by default. Whereas MySQL drivers often disable multiple statements by default. That is to help avoid sql injection. So in that case you must to enable multiple statements explicitly in your connection settings. Otherwise, you cannot use multiple statements. There are also some databases (usually desktop ones like MS Access) that do not support multiple statements at all. So I do not think there is a blanket answer to this question.

If the two insert/update statements are related, you should definitely use a cftransaction as Sam suggested. That ensures the statements are treated as a single unit: ie Either they all succeed or they all fail. So you are not left with partial or inconsistent data. In order to accomplish that, a single connection will be used for both queries in the transaction.

I think every time we call cfquery we
are opening new connection DB

As Sam mentioned, that depends on your settings and whether you are using cftransaction. If you enable Maintain Connections (under Datasource settings in the CF Administrator) CF will maintain a pool of open connections. So when you run a query, CF just grabs an open connection from the pool, rather than opening a new one each time. When using cftransaction, the same connection should be used for all queries. Regardless of whether Maintain Connections is enabled or not.

暗地喜欢 2024-08-21 11:11:13

在数据源设置中,您可以通过“维护连接”设置来告诉它是否保持连接打开。

我相信,由于担心 SQL 注入,ColdFusion 8 数据源首先设置为一次仅运行一个查询。要更改此设置,您需要使用连接字符串进行修改。

最好的选择是打开“维护连接”,如果需要,请使用 cftransaction:

<cftransaction>
<cfquery name="ins" datasource="dsn">
insert into table1 values(<cfqueryparam value="#url.x#">)
</cfquery>
<cfquery name="ins" datasource="dsn">
insert into table2 values(<cfqueryparam value="#url.x#">)
</cfquery>
</cftransaction>

并且始终对用户提交的值使用 cfqueryparam。

Within the data source settings you can tell it whether to keep connections open or not with the Maintain Connections setting.

Starting with, I believe, ColdFusion 8 datasources are set up to run only one query at a time due to concerns with SQL injection. To change this you would need to modify with the connection string.

Your best bet is to turn on Maintain Connections and if needed use cftransaction:

<cftransaction>
<cfquery name="ins" datasource="dsn">
insert into table1 values(<cfqueryparam value="#url.x#">)
</cfquery>
<cfquery name="ins" datasource="dsn">
insert into table2 values(<cfqueryparam value="#url.x#">)
</cfquery>
</cftransaction>

And always, always use cfqueryparam for values submitted by users.

眼眸里的那抹悲凉 2024-08-21 11:11:13

CF8 中的 mySQL 驱动程序现在允许多个语句。
正如 Sam 所说,您可以使用 将许多语句组合在一起
或在 Coldfusion 管理员中 |数据与服务 |数据来源,
添加
allowMultiQueries=true
到连接字符串字段

the mySQL driver in CF8 does now allow multiple statements.
as Sam says, you can use to group many statements together
or in the coldfusion administrator | Data & Services | Data sources,
add
allowMultiQueries=true
to the Connection String field

风铃鹿 2024-08-21 11:11:13

我没有 CF 服务器可以尝试,但 IIRC 应该可以正常工作。

例如:

<cfquery name="doubleInsert" datasource="dsn">
insert into table1 values(x,y,z)
insert into table1 values(a,b,c)
</cfquery>

如果您想要更具体的示例,您将必须提供更具体的信息。

编辑:感谢@SamFarmer:比我使用过的新版本的 CF 可能会阻止这种情况

I don't have CF server to try, but it should work fine IIRC.

something like:

<cfquery name="doubleInsert" datasource="dsn">
insert into table1 values(x,y,z)
insert into table1 values(a,b,c)
</cfquery>

if you want a more specific example you will have to give more specific information.

Edit: Thanks to @SamFarmer : Newer versions of CF than I have used may prevent this

后来的我们 2024-08-21 11:11:13

对死灵感到抱歉(我是该网站的新手)。

你没有提到你正在使用什么数据库。如果您碰巧使用 mySQL,您可以添加最大堆大小允许的任意数量的记录。

我经常使用默认堆大小一次插入最多约 4500 条记录(但这取决于您拥有的数据量)。

INSERT INTO yourTable (x,y,z) VALUES ('a','b','c'),('d','e','f'),('g','h','i')

在我看来,所有数据库都应该这样做。

华泰

Sorry for the Necro (I'm new to the site).

You didn't mention what DB you're using. If you happen to use mySQL you can add as many records as the max heap size will allow.

I regularly insert up to ~4500 records at a time with the default heap size (but that'll depend on the amount of data you have).

INSERT INTO yourTable (x,y,z) VALUES ('a','b','c'),('d','e','f'),('g','h','i')

All DBs should do this IMO.

HTH

心碎无痕… 2024-08-21 11:11:13

使用 CFTRANSACTION 将多个查询分组到一个单元中。

任何使用 CFQUERY 执行并放置在 和 标记之间的查询都将被视为单个事务。在事务块中的所有操作成功执行之前,这些查询请求的数据更改不会提交到数据库。如果查询中发生错误,则事务块中先前查询所做的所有更改都将回滚。

使用 ISOLATION 属性可以对数据库引擎在事务期间执行锁定的方式进行额外控制。

有关详细信息,请访问 http://www.adobe.com/livedocs/ Coldfusion/5.0/CFML_Reference/Tags103.htm

Use CFTRANSACTION to group multiple queries into a single unit.

Any queries executed with CFQUERY and placed between and tags are treated as a single transaction. Changes to data requested by these queries are not committed to the database until all actions within the transaction block have executed successfully. If an error occurs in a query, all changes made by previous queries within the transaction block are rolled back.

Use the ISOLATION attribute for additional control over how the database engine performs locking during the transaction.

For more information visit http://www.adobe.com/livedocs/coldfusion/5.0/CFML_Reference/Tags103.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文