Dan and I are back from the
MySQL for Developers training, and we decided to share the enlightenment. Instead of boring you with a summary of what we've learned, we've decided to divvy up our notes into various "tips & tricks" posts.
Let's say that a client sends you an Access, Excel, or CSV file and tells you to stick all that information into your database. The client is oblivious to the structure of your table, and the file contains several columns that aren't needed. Some of the other columns are improperly formatted, making your life that much harder.
Your first instinct might be to write a PHP (or Perl, or Python, etc) script that squeezes and trims data to the right dimensions, then sends to MySQL. That's fine, but many don't know that MySQL can usually do this tedius work for you. It might also be worth mentioning that MySQL parses this data
exceedingly fast.
LOAD DATA INFILE (or
LOAD LOCAL DATA INFILE from your local machine) is an importer's dream come true. It allows for MySQL itself to load and parse a file. Once loaded, you can set the
FIELDS TERMINATED BY,
ENCLOSED BY,
LINES TERMINATED BY, and
IGNORE ... LINES parameters to split the data into meaningful chunks.
Setup:
LOAD DATE INFILE '[file.ext]'
INTO TABLE [table_name]
FIELDS TERMINATED BY '[column_separator]'
ENCLOSED BY '[usually doublequote]'
LINES TERMINATED BY '[usually \r or \n]'
IGNORE [lines_to_skip]
LINES
(col1, @variable2, col3, col4)
SET col2 = SOME_FUNCTION(@variable2, some_parameters);
You can use
@variables instead of columns for processing. If unneeded file columns exist, then we store that column into a dummy variable (look at
@skip below).
The import file uses "Apr 20 1982" for the birthday, but we want it looking like "1982-04-20". Below, we set the
@birthday variable equal to the file's birthday column, then
SET birthday (the DB column) equal to STR_TO_DATE(@birthday, 'input_format').
Real-world usage:
LOAD DATA INFILE 'import.csv'
INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 2 LINES
(id, @skip, @fname, @lname, @skip, @birthday)
SET name = CONCAT(@fname, ' ', $lname),
birthday = STR_TO_DATE(@birthday, '%b %e %Y');
NOTE: STR_TO_DATE expects the input date format to be specified. The 2nd parameter contains the curious string '%b %e %Y'. In this example, it's looking for a date formatted like
'Jan 9 2001'. The meaning of those special characters can be found
here.
I would highly recommend attending the MySQL training seminars.
David Swain, our trainer, was highly competent and had helpful answers and further insight to every question we could throw at him.
Comments
Mon, 21.04.2008 13:43
Hi Vikram-- Have you set up your repository in Subversive and successfully connected?
Mon, 21.04.2008 12:56
On checkout as.. dialog you asked to choose "Check out as a project configured using the New Project Wizard." That [...]
Fri, 11.04.2008 06:06
Excellent post, very detailed and I especially like the images. It helped me a lot with my set up.
Thu, 03.04.2008 01:55
How can they say that they're "not responsible" for "death, personal injury, or severe property or environmental [...]
Tue, 01.04.2008 23:29
Hi Marcel-- Make sure you follow the pre-step I have above and ensure you've installed one of the two Subversion [...]
Tue, 01.04.2008 20:21
Please help when I try to import, I don't have Other/Checkout Projects from SVN. Where is my problem?
Thu, 21.02.2008 05:58
Great, this was useful.
Wed, 13.02.2008 09:26
Thanks , I was trying hard & hard with http://www.eclipse.org/pdt/ins tall.php but success came here... thanks for the [...]
Wed, 06.02.2008 15:39
I had to find packages for e Redhat EL3 server, the following worked for me: Catdoc [...]
Wed, 30.01.2008 14:38
Yeah, it does. Just remember that a function is specific to the database in which it's created. If you're trying to call [...]
Wed, 30.01.2008 14:17
Awesome - I didn't know you could calculate and transform fields using functions that way. Does it work with custom [...]
Tue, 22.01.2008 20:49
Hehe... DNS hosting services I toyed with including in the tutorial but it's really a bit too much for the one-man (or [...]
Sat, 19.01.2008 18:51
Awesome, thanks very much for the guide!
Wed, 16.01.2008 14:21
I'd have to say that I like #2, its much more of a natural web experience to me. We don't always have to emulate [...]
Wed, 02.01.2008 16:10
It's fashionable this time of year to create some sort of year-end "best of" or "top ten" list [...]