Mysql command question..

Discussion in 'Server Operation' started by jsb1022, Dec 3, 2007.

  1. jsb1022

    jsb1022 New Member

    Can anyone please tell me the mysql command to execute a query to select records and export them into a csv file?

    Thanks,
    jsb1022
     
  2. jsb1022

    jsb1022 New Member

    Converting txt to csv

    I found the command to export the data from mysql to a txt file. Now I need the command to convert the txt file to a .csv format?
     
  3. sjau

    sjau Local Meanie Moderator

    how do you export it to a .txt file?

    A .csv file isn't that much of a difference, instead of (probably tabs) you use commans to seperate the fields... but then I'd rather use the pip | as it is not a standard character.
     
  4. jsb1022

    jsb1022 New Member

    Mysql

    The below is the script I am using to place the .cdr into a .txt file. My question is how do I find the .txt file? I cannot find it. I looked in the tmp folder but its not there....

    select * into outfile 'master.txt' fields terminated by ',' from
    sigpro.cdr where billsec > '0' and calldate between '20071101' and
    '20071201';

    Then I need to convert the txt to a csv in order to FTP to a website. This seems a bit advanced for a junior linux person and I'm mainly windows.

    Thanks
     
  5. sjau

    sjau Local Meanie Moderator

    how do you run that script that you posted?

    I'd create a PHP script to do an export...

    Here's one I use:

    Code:
    <?
    
    // MySQL Connection details
    require_once ('../config.inc.php');
    
    // Connect to DB
    mysql_connect($INFO['sql_host'],$INFO['sql_user'] ,$INFO['sql_pass'])
            or die("Unable to connect to SQL server!");
    mysql_select_db($INFO['sql_database'])
            or die("Unable to select database!");
    
    unset($output);
    $header = "id\tname\tfirstname\tstreet1\tstreet2\tzip\tcity\temail";
    
    // Get info from Members Table and Build Member ID Array
    $sql = "SELECT * FROM members ORDER BY id ASC";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $output .= $row["id"] . "\t" . $row["name"] . "\t" . $row["firstname"] . "\t" . $row["street1"] . "\t" . $row["street2"] . "\t" . $row["zip"] . "\t" . $row["city"] . "\t" . $row["email"] . "\n";
    }
    
    $output = $header . $output;
    
    $fp = fopen ("data.txt", "wb");
    fwrite ( $fp, $output);
    fclose($fp);
    
    header("Content-type: application/txt");
    header("Content-Disposition: attachment; filename=data.txt");
    readfile('data.txt');
    
    ?>
    
    You'll be prompted for a TAB-separated .txt file for downlaoding.
     
    Last edited: Dec 4, 2007
  6. jsb1022

    jsb1022 New Member

    Just log into mysql and run it
     
  7. sjau

    sjau Local Meanie Moderator

    I'd do it with php ;)
     
  8. volksman

    volksman New Member

    If you have phpmyadmin installed it will output to csv. Otherwise is either Perl, PHP or Python to do what you want I believe.....
     
  9. jsb1022

    jsb1022 New Member

    Well I am very basic with Linux, i dont know how to tell if PHP admin is installed.
    Whats the easiest way to convert a txt file to csv in linux?
     
  10. volksman

    volksman New Member

    its a web interface to mysql. if you aren't sure you prolly don't have it. if you have a cpanel based site or a virtual hosted site you may but you'd have to look around in the control panels or ask your provider.

    The script sjau provided would do the trick with a little customization:

    Code:
    mysql_connect($INFO['sql_host'],$INFO['sql_user'] ,$INFO['sql_pass'])
            or die("Unable to connect to SQL server!");
    mysql_select_db($INFO['sql_database'])
            or die("Unable to select database!");
    Change sql_host, sql_user, sql_pass and sql_database to their respective values for your DB.

    Code:
    $header = "id\tname\tfirstname\tstreet1\tstreet2\tzip\tcity\temail";
    change id name firstname etc to your table headers and replace \t with ,

    Code:
    $sql = "SELECT * FROM members ORDER BY id ASC";
    change that to be a select * from <yourtable> and order however you want.
    Code:
    $output .= $row["id"] . "\t" . $row["name"] . "\t" . $row["firstname"] . "\t" . $row["street1"] . "\t" . $row["street2"] . "\t" . $row["zip"] . "\t" . $row["city"] . "\t" . $row["email"] . "\n";
    Again change id name etc to your own table column names (add and remove as needed just follow the patterns). Again replace any instance of \t with a ,

    Then put that script on your web server and call the script directly in a web browser....so for example you can call it dump.php and put it in the root of your web site....then call http://yourwebsite.com/dump.php and save the file it presents....

    I would suggest you remove the script as soon as you are done cause anyone can get a dump of your db otherwise....
     
  11. jsb1022

    jsb1022 New Member

    Ok, so when I am logged into mysql and I run this:

    select * into outfile 'master.txt' fields terminated by ',' from
    sigpro.cdr where billsec > '0' and calldate between '20071101' and
    '20071201';

    Where do I find the master.txt file?
    Then, how would I convert the master.txt file to a csv?

    jsb1022
     
  12. jsb1022

    jsb1022 New Member

    I was able to pull the data I need from the database in mysql in a txt file and I moved it from the tmp folder to a folder I created in the /home directory. I did this b/c I wasnt sure if the linux server is rebooted if the tmp directory gets deleted. Anyway, I now need to conver this txt file over to a csv file so I can FTP it to someone.
     
  13. sjau

    sjau Local Meanie Moderator

    change the file extension from .txt to .csv
     
  14. jsb1022

    jsb1022 New Member

    Thats not the answer here. You cannot just change a .txt file to a .csv file. It will not work b/c of the comma delimiter needed.
     
  15. sjau

    sjau Local Meanie Moderator

    csv --> comma separated values

    as you already did separate with commas it is a .csv file with a .txt extension.
     

Share This Page