<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NonStopSites Blog &#187; web admin</title>
	<atom:link href="http://blog.nonstopsites.us/tag/web-admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nonstopsites.us</link>
	<description>Dynamic Websites Done Right!</description>
	<lastBuildDate>Sat, 24 Jul 2010 06:02:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Perl to parse CSV files</title>
		<link>http://blog.nonstopsites.us/using-perl-to-parse-csv-files/</link>
		<comments>http://blog.nonstopsites.us/using-perl-to-parse-csv-files/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 07:43:30 +0000</pubDate>
		<dc:creator>Sech</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[web admin]]></category>

		<guid isPermaLink="false">http://blog.nonstopsites.us/2008/03/08/using-perl-to-parse-csv-files/</guid>
		<description><![CDATA[It took me a while to get this right. For future reference and for others who may need the same functionality here is the information: Task: 1. Upload a CSV file to the server, 2. Use a CRON job to run a perl script that inserts the fields in the CSV file to a MYSQL [...]]]></description>
			<content:encoded><![CDATA[<!-- sphereit start --><p>It took me a while to get this right. For future reference and for others who may need the same functionality here is the information:</p>
<p>Task:</p>
<p>1. Upload a CSV file to the server,<br />
2. Use a CRON job to run a perl script that inserts the fields in the CSV file to a MYSQL database file.</p>
<p>Solution 1:</p>
<blockquote><p>#!/usr/bin/perl</p>
<p>use DBI;<br />
my $DSN = 'DBI:mysql:database';<br />
my $dsn_user = 'database_user';<br />
my $dsn_pw = 'database_user_password';<br />
my $dbh = DBI-&gt;connect($DSN,$dsn_user,$dsn_pw)<br />
or die "Couldn't connect to database: " . DBI-&gt;errstr;</p>
<p>$rows = $dbh-&gt;do("DELETE FROM table");</p>
<p>$result = $dbh-&gt;do("OPTIMIZE TABLE table");</p>
<p># Load local comma separated, fields enclosed by quotes text database<br />
#- File has to be in the same directory of this file or give full directory path<br />
$result = $dbh-&gt;do("LOAD DATA LOCAL INFILE '/full_directory_path_to/data.csv' INTO TABLE table FIELDS TERMINATED BY ','ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (table_field1,table_field2,table_field3,so_on)");</p></blockquote>
<p>This solution worked great on a server with a MYSQL 3.23</p>
<p>Challenge:</p>
<p>Time to move to a server with a MYSQL 4 or 5 where -local infile is disabled and it is not possible to get the web host do the necessary changes. You need a work-around fast... It is not easy to find a CSV parser in Perl that will parse a CSV file with comma separated columns that also has several columns that are enclosed by quotation marks ("). After trying several different solutions that either not worked at all or required major changes I found this here:</p>
<p><a href="http://coding.derkeiler.com/Archive/Perl/comp.lang.perl.misc/2004-07/1638.html">http://coding.derkeiler.com/Archive/Perl/comp.lang.perl.misc/2004-07/1638.html</a></p>
<blockquote><p>sub parseCSVLine{<br />
my $line=shift;<br />
my @fields=();my $field=''; # initialize<br />
my @fragments=split(/,/,$line,-1); # split into "," seperated fragments<br />
foreach my $nibble(@fragments){<br />
if($field){$field .= ','} # add the missing commas;<br />
$field .= $nibble; # combine fragment into a field until...<br />
my $count = $field =~tr/"/"/; # count quotes<br />
unless($count % 2){ # ...there's an even number of double quotes<br />
$field =~s/""/"/g; $field=~s/^\s*"//g; $field=~s/"\s*$//g; # fix quotes<br />
# fix quotes<br />
push @fields, $field;<br />
$field=''; # reinitialize $field<br />
}<br />
}<br />
return @fields;<br />
}</p></blockquote>
<p>Surprisingly this worked without a hitch. After you run each line through this subroutine do a REPLACE into the database table.</p>
<!-- sphereit end --><span style="margin-bottom:40px; border-bottom:none;"><a class="iconsphere" title="Sphere: Related Content" onclick="return Sphere.Widget.search('http://blog.nonstopsites.us/using-perl-to-parse-csv-files/')" href="http://www.sphere.com/search?q=sphereit:http://blog.nonstopsites.us/using-perl-to-parse-csv-files/">Sphere: Related Content</a></span><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://blog.nonstopsites.us/using-perl-to-parse-csv-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
