Question

I'm trying to create a function to create csv files from queries. After I run the query, I'm looping through it and appending each row's fields to a StringBuffer object. To that end, I'm putting the column names into an array:

<cfset indexes = #ListToArray(Arguments.header)# />

where the argument is currently a string like:

"col1, col2, col3...."

I've verified that both the query and the array are what they should be by dumping. The trouble comes when looping through the query results. Given:

<cfset indexes_length = #ArrayLen(indexes)# />
<cfloop query="query_for_csv">
        <cfloop index="i" from="1" to="#indexes_length#">
            <cfset attr = #indexes[i]# />
            <cfset sbOutput.Append(
                "#query_for_csv[attr][query_for_csv.CurrentRow]#") />
        </cfloop>           
    </cfloop> 

Only the first value of the first row is output before I get the error message:

[Table (rows 10 columns col1, col2, col3):
[col1: coldfusion.sql.QueryColumn@6f731eba]
[col2: coldfusion.sql.QueryColumn@6ee67e7f] 
[col3: coldfusion.sql.QueryColumn@5c6647cb] 
is not indexable by col2 

If I replace the variable #attr# with the literal "col2":

#query_for_csv['col2'][query_for_csv.CurrentRow]#

then the loop sails through with no problem, and spits out all the values indexed by 'col2'. Any ideas?

Was it helpful?

Solution

I would guess it's the spaces in your header list that is the problem, so probably this would work:

<cfset attr = trim(indexes[i]) />


However, since you're not using them, you probably don't need that and can just do this...

<cfloop query="QueryName">
    <cfloop index="CurCol" list=#QueryName.ColumnList# >
        <cfset sbOutput.Append(
            QueryName[CurCol][QueryName.CurrentRow]
        )/>
    </cfloop>
</cfloop>


p.s.
You'll note here that there's only one pair of hashes - there only needs to be one pair in your original code snippets too (in the to attribute) - the rest are unnecessary noise.

OTHER TIPS

As has already been said before, try to avoid spaces before or after a list element.

In case you want to compare notes, check out the approach Ben Nadel chose to implement such a Query2CSV converter: http://www.bennadel.com/blog/1239-Updated-Converting-A-ColdFusion-Query-To-CSV-Using-QueryToCSV-.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top