Converting a .CSV file to a query using the CFHTTP tag in ColdFusion

Readability

Converting a .CSV file to a query using the CFHTTP tag in ColdFusion

The fol­low­ing exam­ple shows how you can con­vert an exter­nal .CSV (comma sep­a­rated val­ues) file into a query using the <CFHTTP> tag in Cold­Fu­sion and set­ting the name, url, and firstRowAsHeaders attributes.




The exter­nal .CSV file, art.csv, is as follows:

"ARTID","ARTISTID","ARTNAME","DESCRIPTION","ISSOLD","LARGEIMAGE","MEDIAID","PRICE"
"1","1","charles1","Pastels/Charcoal","1","aiden01.jpg","1","10000"
"2","1","Michael","Pastels/Charcoal","0","aiden02.jpg","1","13900"
"3","1","Freddy","Pastels/Charcoal","1","aiden03.jpg","1","12500"
"4","1","Paulo","Pastels/Charcoal","1","aiden04.jpg","1","11100"
"5","1","Mary","Pastels/Charcoal","1","aiden05.jpg","1","13550"

And the out­put of the <CFDUMP> tag is as fol­lows:

If you want to con­vert a query to CSV string, see Ben Nadel’s awe­some blog post, “Updated: Con­vert­ing A Cold­Fu­sion Query To CSV Using Query­ToCSV()”.

The following example shows how you can convert an external .CSV (comma separated values) file into a query using the <CFHTTP> tag in ColdFusion and setting the name, url, and firstRowAsHeaders attributes.

<cfhttp name="artFromCSV"
        url="http://live.coldfusionexamples.com/art.csv"
        firstRowAsHeaders="true"
        method="get" />
 
<cfdump var="#artFromCSV#" />

The external .CSV file, art.csv, is as follows:

"ARTID","ARTISTID","ARTNAME","DESCRIPTION","ISSOLD","LARGEIMAGE","MEDIAID","PRICE"
"1","1","charles1","Pastels/Charcoal","1","aiden01.jpg","1","10000"
"2","1","Michael","Pastels/Charcoal","0","aiden02.jpg","1","13900"
"3","1","Freddy","Pastels/Charcoal","1","aiden03.jpg","1","12500"
"4","1","Paulo","Pastels/Charcoal","1","aiden04.jpg","1","11100"
"5","1","Mary","Pastels/Charcoal","1","aiden05.jpg","1","13550"

And the output of the <CFDUMP> tag is as follows:

If you want to convert a query to CSV string, see Ben Nadel‘s awesome blog post, “Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()”.

Leave a Reply

Your email address will not be published.