我可以在 ColdFusion 中按索引获取查询行吗?

发布于 2024-07-29 11:40:02 字数 267 浏览 6 评论 0原文

我想获取 ColdFusion 查询对象中的特定行而不对其进行循环。

我想做这样的事情:

<cfquery name="QueryName" datasource="ds">
SELECT *
FROM    tablename
</cfquery>

<cfset x = QueryName[5]>

但是它给了我一个错误,说查询不能按“5”进行索引。 我知道这个查询中有超过 5 条记录。

I want to get a specific row in a ColdFusion Query object without looping over it.

I'd like to do something like this:

<cfquery name="QueryName" datasource="ds">
SELECT *
FROM    tablename
</cfquery>

<cfset x = QueryName[5]>

But it's giving me an error saying that the query isn't indexable by "5". I know for a fact that there are more than 5 records in this query.

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

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

发布评论

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

评论(8

素罗衫 2024-08-05 11:40:02

您无法获得 CF <= 10 中的行。您必须获得特定的列。

<cfset x = QueryName.columnName[5]>

然而,距离我发布这个答案已经过去了 8 年。 显然CF11最终实现了这个功能。 请参阅此答案

You can't get a row in CF <= 10. You have to get a specific column.

<cfset x = QueryName.columnName[5]>

It's been 8 years since I posted this answer, however. Apparently CF11 finally implemented that feature. See this answer.

归途 2024-08-05 11:40:02

现在可以在 Coldfusion 11 中通过 QueryGetRow< 来完成此操作/a>

<cfquery name="myQuery" result="myresult" datasource="artGallery" fetchclientinfo="yes" >
select * from art where ARTID >
<cfqueryparam value="2" cfsqltype="CF_SQL_INTEGER">
</cfquery>

<cfdump var="#myQuery#" >

<cfset data = QueryGetRow(myQuery, 1) >

<cfdump var="#data#" >

This can now be accomplished in coldfusion 11 via QueryGetRow

<cfquery name="myQuery" result="myresult" datasource="artGallery" fetchclientinfo="yes" >
select * from art where ARTID >
<cfqueryparam value="2" cfsqltype="CF_SQL_INTEGER">
</cfquery>

<cfdump var="#myQuery#" >

<cfset data = QueryGetRow(myQuery, 1) >

<cfdump var="#data#" >
时光是把杀猪刀 2024-08-05 11:40:02

我认为有一个更简单的解决方案......
我猜您知道您的列名称,并且只想要这一列或那一列。 那么您不需要将整行放入结构中。 您可以按行号引用查询(记住它是基于 1 而不是 0)。

yourQueryName["yourColumnName"][rowNumber]

<cfoutput>
     #mycontacts["Name"][13]#
     #mycontacts["HomePhone"][13]# 
</cfoutput>

I think there is a simpler solution...
I am guessing you know your column names and only want this column or that one. Then you don't need to put the whole row in a struct. You can reference the query by row number (remember its 1 based not 0).

yourQueryName["yourColumnName"][rowNumber]

<cfoutput>
     #mycontacts["Name"][13]#
     #mycontacts["HomePhone"][13]# 
</cfoutput>
浊酒尽余欢 2024-08-05 11:40:02

您必须首先将查询转换为结构:

<cfscript>
    function GetQueryRow(query, rowNumber) {
        var i = 0;
        var rowData = StructNew();
        var cols = ListToArray(query.columnList);
        for (i = 1; i lte ArrayLen(cols); i = i + 1) {
            rowData[cols[i]] = query[cols[i]][rowNumber];
        }
        return rowData;
    }
</cfscript>

<cfoutput query="yourQuery">
    <cfset theCurrentRow = GetQueryRow(yourQuery, currentRow)>
    <cfdump var="#theCurrentRow#">
</cfoutput>

希望这能为您指明正确的方向。

You have to convert the query to a struct first:

<cfscript>
    function GetQueryRow(query, rowNumber) {
        var i = 0;
        var rowData = StructNew();
        var cols = ListToArray(query.columnList);
        for (i = 1; i lte ArrayLen(cols); i = i + 1) {
            rowData[cols[i]] = query[cols[i]][rowNumber];
        }
        return rowData;
    }
</cfscript>

<cfoutput query="yourQuery">
    <cfset theCurrentRow = GetQueryRow(yourQuery, currentRow)>
    <cfdump var="#theCurrentRow#">
</cfoutput>

Hope this points you in the right direction.

鹿港巷口少年归 2024-08-05 11:40:02

我知道每当我谷歌“cfquery 括号符号”时我都会回到这个线程。 这是我编写的一个函数,用于使用括号表示法处理这种情况。 希望这也可以帮助其他人:

<cffunction name="QueryGetRow" access="public" returntype="array" hint="I return the specified row's data as an array in the correct order">
    <cfargument name="query" required="true" type="query" hint="I am the query whose row data you want">
    <cfargument name="rowNumber" required="true" hint="This is the row number of the row whose data you want">

    <cfset returnArray = []>
    <cfset valueArray = []>

    <cfset cList = ListToArray(query.ColumnList)>
    <cfloop from="1" to="#ArrayLen(cList)#" index="i">
        <cfset row = query["#cList[i]#"][rowNumber]>
        <cfset row = REReplace(row, "(,)", " ")>
        <cfset returnArray[i] = row>
        <cfset i++>
    </cfloop>   
    <cfreturn returnArray>
</cffunction>

REReplace 是可选的,我用它来清理逗号,这样如果您以后必须使用它,它就不会搞砸 arrayToList 函数。

I know I come back to this thread any time I Google "cfquery bracket notation". Here's a function I wrote to handle this case using bracket notation. Hopefully this can help someone else too:

<cffunction name="QueryGetRow" access="public" returntype="array" hint="I return the specified row's data as an array in the correct order">
    <cfargument name="query" required="true" type="query" hint="I am the query whose row data you want">
    <cfargument name="rowNumber" required="true" hint="This is the row number of the row whose data you want">

    <cfset returnArray = []>
    <cfset valueArray = []>

    <cfset cList = ListToArray(query.ColumnList)>
    <cfloop from="1" to="#ArrayLen(cList)#" index="i">
        <cfset row = query["#cList[i]#"][rowNumber]>
        <cfset row = REReplace(row, "(,)", " ")>
        <cfset returnArray[i] = row>
        <cfset i++>
    </cfloop>   
    <cfreturn returnArray>
</cffunction>

The REReplace is optional, I have it in there to cleanse commas so that it doesn't screw up the arrayToList function later on if you have to use it.

丑疤怪 2024-08-05 11:40:02

我想从查询中提取一行,并保留列名(当然)。 这就是我解决它的方法:

<cffunction name="getQueryRow" returntype="query" output="no">
    <cfargument name="qry" type="query" required="yes">
    <cfargument name="row" type="numeric" required="yes">
    <cfset arguments.qryRow=QueryNew(arguments.qry.columnlist)>
    <cfset QueryAddRow(arguments.qryRow)>
    <cfloop list="#arguments.qry.columnlist#" index="arguments.column">
        <cfset QuerySetCell(arguments.qryRow,arguments.column,Evaluate("arguments.qry.#arguments.column#[arguments.row]"))>
    </cfloop>
    <cfreturn arguments.qryRow>
</cffunction>

I wanted to extract a single row from a query, and keeping the column names (of course). This is how I solved it:

<cffunction name="getQueryRow" returntype="query" output="no">
    <cfargument name="qry" type="query" required="yes">
    <cfargument name="row" type="numeric" required="yes">
    <cfset arguments.qryRow=QueryNew(arguments.qry.columnlist)>
    <cfset QueryAddRow(arguments.qryRow)>
    <cfloop list="#arguments.qry.columnlist#" index="arguments.column">
        <cfset QuerySetCell(arguments.qryRow,arguments.column,Evaluate("arguments.qry.#arguments.column#[arguments.row]"))>
    </cfloop>
    <cfreturn arguments.qryRow>
</cffunction>
纵性 2024-08-05 11:40:02

前面描述的通过列名和行号获取查询数据的方法(variables.myquery["columnName"][rowNumber])是正确的,但不方便获取整行查询数据。

我正在运行 Railo 4.1。 这是一个很酷的解决方案。 遗憾的是,这无法按照我们想要的方式直接获取整行数据,但以下方法允许我们通过一些步骤获得我们想要的内容。

当您 serializeJSON(variables.myquery) 时,它会将查询更改为 JSON 格式的 cfml 结构对象,其中包含两项:“Columns”和“Data”。 这两个都是数据数组。 “数据”数组是一个二维数组,先包含行数据,然后包含列数据。

问题是现在我们有一个无法使用的字符串。 然后,如果我们重新序列化它,它不是一个查询,而是上述格式的可用常规结构。

假设我们已经有一个名为“variables.myquery”的查询变量。 然后看下面的代码:

<cfset variables.myqueryobj = deserializeJSON(serializeJSON(variables.myquery)) />

现在,您可以通过以下方式获得二维数组:

<cfset variables.allrowsarray = variables.myqueryobj.data />

通过以下方式获得一个查询行数组:

<cfset variables.allrowsarray = variables.myqueryobj.data[1] />

OR 最后一行:

<cfset variables.allrowsarray = variables.myqueryobj.data[variables.myquery.recordCount] />

您可以通过列顺序编号迭代获得各个列值:

<cfset variables.allrowsarray = variables.myqueryobj.data[1][1] />

现在对于大型查询结果来说,这可能很慢并且可能不明智,但这仍然是一个很酷的解决方案。

Methods previously described for obtaining query data by column name and row number (variables.myquery["columnName"][rowNumber]) are correct, but not convenient for getting a full row of query data.

I'm running Railo 4.1. And this is a cool solution. Too bad this can't be done the way we would want outright to get a full row of data, but the following method allows us to get what we want through a few hoops.

When you serializeJSON(variables.myquery) it changes the query to a JSON formatted cfml struct object with two items: "Columns" and "Data". Both of these are arrays of data. The "data" array is a two-dimensional array for rows and then columnar data.

The issue is that now we have an unusable string. Then if we re-serialize it it's NOT a query, but rather usable regular struct in the format described above.

Assume we already have a query variable named 'variables.myquery'. Then look at the following code:

<cfset variables.myqueryobj = deserializeJSON(serializeJSON(variables.myquery)) />

Now you get the two dimensional array by getting this:

<cfset variables.allrowsarray = variables.myqueryobj.data />

And you get one query row array by getting this:

<cfset variables.allrowsarray = variables.myqueryobj.data[1] />

OR the last row this way:

<cfset variables.allrowsarray = variables.myqueryobj.data[variables.myquery.recordCount] />

And you can get individual column values by column order number iteration:

<cfset variables.allrowsarray = variables.myqueryobj.data[1][1] />

Now this might be slow and possibly unwise with large query results, but this is a cool solution nonetheless.

北斗星光 2024-08-05 11:40:02

查看 queryGetRow 的文档。 它接受一个查询对象和一个行索引,其中第一行的索引为 1(非 0)。这种方式使用的索引必须是正整数。

<cfquery name="QueryName" datasource="ds">
  SELECT *
  FROM tablename
</cfquery>

<!---
    This would retrieve the first record of the query
    and store the record in a struct format in the variable 'x'.
--->
<cfset x = queryGetRow(QueryName, 1) />
<!---
    This is an alternative using the member method form of queryGetRow
--->
<cfset x = QueryName.getRow(1) />

Check out the documentation for queryGetRow. It accepts a query object and an index of the row with the first row being referenced with the index of 1 (NOT 0) The index used this way is required to be a positive integer.

<cfquery name="QueryName" datasource="ds">
  SELECT *
  FROM tablename
</cfquery>

<!---
    This would retrieve the first record of the query
    and store the record in a struct format in the variable 'x'.
--->
<cfset x = queryGetRow(QueryName, 1) />
<!---
    This is an alternative using the member method form of queryGetRow
--->
<cfset x = QueryName.getRow(1) />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文