Skip to Content
Learning MySQL
book

Learning MySQL

by Saied M.M. Tahaghoghi, Hugh E. Williams
November 2006
Beginner
620 pages
19h 47m
English
O'Reilly Media, Inc.
Content preview from Learning MySQL

Importing and Exporting Data

From time to time, you may need to transfer data into the database from external sources, or to generate data in a format that other applications can use. A common file format for this is the comma-separated values (CSV) format discussed in Loading Data from Comma-Delimited Files” in Chapter 8. Data import and export is one of the areas in which Perl is very strong, and programs in Perl can read and write data in a large number of formats. For example, you can generate plain text, HTML, XML, or Rich Text Format (RTF) documents. RTF documents are more complex but can contain formatting instructions that most word processors understand. There are even Perl modules to process binary (nontext) formats, such as the Microsoft Excel spreadsheet file format.

Earlier, in Binding Variables to a Query,” we explained how to import data from a CSV file. Let’s now look at an example to export data from our Animals database to a CSV file. All we need to do is to use the print statement to write to the output file, with the data separated by a comma, as shown in Example 17-6.

Example 17-6. Perl script to export data from the Animals database, using binding

#!/usr/bin/perl
use DBI;
use strict;

# If the user hasn't provided any command-line arguments, provide a
# helpful error message.
if(@ARGV!=1)
{
    die("Syntax: $0 [Output file]\n");
}

my $DB_Database="AnimalDB";
my $DB_Username="root";
my $DB_Password="the_mysql_root_password"; my $dbh=DBI->connect( "DBI:mysql:host=localhost;database=$DB_Database", ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning MySQL, 2nd Edition

Learning MySQL, 2nd Edition

Vinicius M. Grippa, Sergey Kuzmichev
MySQL Cookbook, 4th Edition

MySQL Cookbook, 4th Edition

Sveta Smirnova, Alkin Tezuysal

Publisher Resources

ISBN: 0596008643Errata Page