Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

writing a file

Posted on 2000-03-31
4
Medium Priority
?
152 Views
Last Modified: 2013-12-24
I need to write some code which will allow a client to query a database and then write the results of the query into a text file (csv) to be placed in the same folder as the database on the server.

The query is easy, but how do I write the text file.

Thanks for the help.
0
Comment
Question by:RICHARDH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:bdemarzo
ID: 2674444
Use CFFILE to write to the text file.

<CFFILE ACTION=WRITE FILE="filename" OUTPUT="output text">

You first need to create the output text, best done in a variable. Let's say you query a database as follows:

<cfquery name="myquery" ...>
SELECT field1, field2
FROM mydbf
</cfquery>

Then create the variable with your CSV text:

<cfoutput query="myquery">
<cfset myoutput=myquery.field1 & "," & myquery.field2>
</cfoutput>

Finally, add the CSV file to your text file:

<cffile action="write" file="c:\myfile.csv" output=#myoutput#>

That should do it! Note that if you want to APPEND to a text file rather than overwriting it, use action="append" in the CFFILE tag.
0
 

Accepted Solution

by:
bdemarzo earned 100 total points
ID: 2674446
And of course, that's my final answer, Regis...
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 2674473
basically do this:

<cfset filename = "/pathtodb/text.csv">
<cfloop query="queryname">
<cfset Result= "#queryname.field1#" & "," "#queryname.field2#" & "," "#queryname.field3#">
<cffile action="APPEND" file="#filename#" output="#Result#" addnewline="Yes">
</cfloop>

If you don't know the column names or field names then do this:
<cfloop index="i" list="queryname.columnlist" delimiters=",">
<cfset Result = "#Result#" & "#queryname[index]#" & ","
</cfloop>
chop of the last ","

I am not sure if the queryname[index]
is right but I can find out the exact syntax if you need it.

CJ
0
 

Author Comment

by:RICHARDH
ID: 2674565
Thanks to both people for the answers. they seem quite different, will they both do the same thing.

thanks to both of you.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question