Solved

Show column headers via export to CSV

Posted on 2011-09-22
11
528 Views
Last Modified: 2012-05-12
Hello,

How do I show column headers via export to CSV format using the OUTFILE command? I can only export data from my query with the data.



please help

0
Comment
Question by:lulubell-b
  • 6
  • 5
11 Comments
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36582652
The OUTFILE command doesn’t support an option to output the headers of the result set you are exporting.

Use sed to insert header into the result set file.

eg: sed -i 'Header1,Header2,Header3' result.csv
0
 

Author Comment

by:lulubell-b
ID: 36582742
please go into more detail. Never used sed
0
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36582766
sed (stream editor) is a Unix/linux utility

Please see : http://en.wikipedia.org/wiki/Sed
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:lulubell-b
ID: 36582820
I'm currently using SQuirreL, will this work in this environment? Is there anyway to perform this dynamically? If the table were to change, I don't want to have to change the script too.
0
 
LVL 1

Accepted Solution

by:
rajeeshb earned 500 total points
ID: 36582930
ok. Try this:-

SELECT 'Column1','Column2','Column2'
UNION
{SELECT Column1, Column2, Column3 INTO OUTFILE 'result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
FROM table_name
ORDER BY Column1 DESC);
0
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36582938
Sorry for typo. Try this :

SELECT 'Column1','Column2','Column3'
UNION
{SELECT Column1, Column2, Column3 INTO OUTFILE 'result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
FROM table_name
ORDER BY Column1 DESC)
0
 

Author Comment

by:lulubell-b
ID: 36583059
Thank you

Is there away to do this in a stored procedure so I can build this dynamically? I just trying to get away from the hard coding.

Thanks
0
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36583075
i guess we canot do this dynamically. Thats the limitation.
0
 

Author Comment

by:lulubell-b
ID: 36583320
I found the table the select column_name from information_schema.columns where table_name = 'A'

Is there a way to use this with the above script to create a stored procedure?
0
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36583421
i know how  to get column names in rows. But make rows to column dynamically is tough.  
0
 

Author Comment

by:lulubell-b
ID: 36588177
Could I do the following? I found it on PHPFreaks.com. Or could I use MYSQLDump to show col headers?


$host = 'localhost';   //your Hostname 
$user = 'xxxxx'; //Database Username 
$pass = 'xxxxx';    //Database Pasword 
$db = 'xxxxx';    //Database name 
$csv_output = "";
$table = 'maptable';   //Table name 
$file = 'csvexport.csv';    //CSV File name
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error()); 
mysql_select_db($db) or die("Can not connect."); 
$query = "SELECT * FROM $table";
$result = mysql_query($query);
$colCount = mysql_num_fields($result);  //this is the colCount
$i = $colCount;

for($j = 0; $j < $i; $j++)
{
   $rowr = mysql_field_name($result, $j);
   $csv_output .= '"' . $rowr . "\",";
}
//$csv_output .= '"' . $rowr. "\"";
$csv_output .= "\n";

while ($rowr = mysql_fetch_row($result))
{
   for ($j=0;$j<$i-1;$j++)
   {
      $csv_output .='"'.$rowr[$j]."\",";
   }
   $csv_output .='"'.$rowr[$j]."\"";
   $csv_output .= "\n";
}
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $csv_output);
fclose($fh);
readfile('csvexport.csv');

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question about DB Schema 27 61
Mysql Left Join Case 10 70
Database needed for HR files 4 57
Complex SQL statement in VB.NET 7 31
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

831 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