|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS Tips & Tricks Generic SQL Table Export
Generic SQL Table Export
By: David Schwartz
Jan. 27, 2000 12:00 AM
In my last article (CFDJ, Vol. 1, issue 6) I presented Array_Table, a universal browser-based table viewer and query engine. Array_Table allows you to browse, edit and query any Microsoft SQL Server table. It's 100% dynamic so it works with any database. Many readers and clients loved the new capability and ease it offered. And they wanted more, of course.
Feeling Trapped?
Freedom At Last! This form shows you a list of all the tables in a data source with the option to view the data or query the table. I've added a choice, "Export", to the table list. This is the only necessary addition to Table_List.cfm (see Listing 1) and it's simply a hot link to the table export program. Here's the code.... <a href="Export/Export_Menu.cfm?TableName=#Table_List.Name#">Export</a> All of the export files are being stored and called from a subfolder, "Export". This keeps your files neat and provides one point of reference if you have to modify any export-related code. When you click on "Export", the Export_menu.cfm script is called and you're presented with the export menu (see Figure 2 and Listing 2). The table you selected is automatically passed to the export menu. Now you have the option of exporting the data to a comma- or tab-delimited file, with or without field names in the file header. If you enter your e-mail address, Array_Export will e-mail the exported data file to you.
How'd He Do That?
<!--- Export.CFM - Passes logon and user choice parameters to the export engine, Table_Export.CFM. ---> Export.cfm sets the directory where the exported data file will be stored. Then it sets the name of the exported file to be the file name plus the current date. So if you were exporting a table called "Sales" on December 6, 1999, the exported file name would be "Sales12061999.txt". Export.cfm then calls Table_Export. cfm and passes all the necessary parameters, such as tablename and filename. Table_Export.Cfm There are four steps in exporting the table:
1. Query the table to retrieve all of the rows.
<cfquery name="Export" datasource="Demo" username="Guest" password="Guest">
Step 2. Get a list of all fields.
<cfquery name="Field_List" datasource="Demo" username="Guest" password="Guest">
Here we query the SQL Server system tables (see December 1999 article) to retrieve the list of fieldnames in the table. We need the field list so we can export the data and separate it by fields. The variable x will hold the list of fields, each fieldname separated by a comma.
<Cffile> is the perfect tag for creating and editing text files. Using the action="write" option, ColdFusion will create the text file if it doesn't already exist.
<!--- Create export file --->
Once the file is created, you can write the field list header to it (if the user chose to add fieldnames to the file).
<cffile action="APPEND" file="#Attributes.FileName#" output="#x##Chr(13)##Chr(10)#" addnewline="No">
Here, the variable x, which now holds the list of fields, is used.
Step 3. Write the data out to a text file in delimited text.
To make it easier to loop through the list of fields, I prefer to use arrays. The following command converts the fieldlist to an array called FL.
<cfset FL = ListToArray(x, #Attributes.
Delimiter#)>
A simple nested (double) loop can now be used to loop through all the records in the table and output all the data. During the loop a single line of data is created in the variable D. It contains all data from all fields as well as the field delimiter character. After the data line is built, it can be written to the text file using <cffile> with the action="append". Append adds new lines to the file without erasing any existing data.
<!--- Loop through the export query which contains all data rows --->
Step 4. Mail the exported file to the user.
After the export loop completes the file, a new export file will be sitting on the server. If the user entered an e-mail address on the Export_menu.cfm form, the file will automatically be e-mailed to them. It doesn't get any easier than this.
Extension Cord, Please CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||