<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Excel (CSV) to MySQL hell</title>
	<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html</link>
	<description>rtraction blog</description>
	<pubDate>Sun, 06 Jul 2008 00:51:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>

	<item>
		<title>By: Sean</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-3832</link>
		<author>Sean</author>
		<pubDate>Fri, 18 Jan 2008 16:04:47 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-3832</guid>
					<description>I export a fairly large amount of data in CSV everyday and import into MySQL. I use a simple PHP script. I’m not saying it will work for you, but it does for me:

&lt;code&gt;mysql_connect("localhost", "user", "password", false, 128) or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());
$datafile = '/path/to/file.csv';
$sql = mysql_query("TRUNCATE TABLE table_name");
$sql = mysql_query("LOAD DATA LOCAL INFILE '$datafile' INTO TABLE table_name FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\r\\n'") or die(mysql_error());&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I export a fairly large amount of data in CSV everyday and import into MySQL. I use a simple PHP script. I’m not saying it will work for you, but it does for me:</p>
<p><code>mysql_connect("localhost", "user", "password", false, 128) or die(mysql_error());<br />
mysql_select_db("DATABASE") or die(mysql_error());<br />
$datafile = '/path/to/file.csv';<br />
$sql = mysql_query("TRUNCATE TABLE table_name");<br />
$sql = mysql_query("LOAD DATA LOCAL INFILE '$datafile' INTO TABLE table_name FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\r\\n'") or die(mysql_error());</code></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: noah</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-3775</link>
		<author>noah</author>
		<pubDate>Fri, 18 Jan 2008 16:13:34 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-3775</guid>
					<description>I've run into similar problems and implemented a slightly different solution. Frustrated with searching for a utility to convert the CSV file, I eventually turned to the command line functions in MySQL. &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/load-data.html" target="_blank" rel="nofollow"&gt;&lt;b&gt;LOAD DATA INFILE&lt;/b&gt;&lt;/a&gt; is the command to quickly load text file into a database table. 

The easiest way to get to the command line client is to open MySQL Query Browser and choose &lt;b&gt;MySQL Command Line Client&lt;/b&gt; from the &lt;b&gt;Tools&lt;/b&gt; menu. You can also get there by typing &lt;b&gt;mysql&lt;/b&gt; at a Windows command prompt; it is the same command in the Mac and Unix terminal. 

Make sure you first select the database you want with the &lt;b&gt;use &lt;i&gt;database_name&lt;/i&gt;&lt;/b&gt; command.

The syntax of &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/load-data.html" target="_blank" rel="nofollow"&gt;&lt;b&gt;LOAD DATA INFILE&lt;/b&gt;&lt;/a&gt; can be a bit overwhelming at first. This is what I found worked best for CSV files exported from MS Access or phpMyAdmin.

&lt;code&gt;LOAD DATA LOCAL INFILE 'c:/import.csv' INTO TABLE tbl_import_data FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' OPTIONALLY ENCLOSED BY '"' IGNORE 1 LINES (column_1, column_2, column_3, column_n);&lt;/code&gt;

There are many more options to this powerful command but the above example will handle the majority of your CSV needs. There are a few things to consider when using it:
&lt;ul style="list-style-type: circle;"&gt;
&lt;li&gt;Fields are usually terminated by a comma but could be a semi-colon, tilde, or whatever delimiter you like.&lt;/li&gt;
&lt;li&gt;Specifying a line terminator may be necessary, depending on the text file format. This often depends on the operating system. Windows uses '\r\n' while Mac and Unix uses '\n', which is the default.&lt;/li&gt;
&lt;li&gt;Optional enclosing quotes are necessary when your text fields are enclosed by quotes, which is often the case by default. The "optional" keyword ensures that the command doesn't die when it encounters a field that isn't enclosed, such as an integer value.&lt;/li&gt;
&lt;li&gt;Ignoring the first line is useful for when you export data with column names in the first row&lt;/li&gt;
&lt;li&gt;Working on a local database may be easier, as special file permissions must be given to the user or the file must be accessible to the MySQL server.&lt;/li&gt;
&lt;li&gt;When loading a local file, you must supply the full path of the file you want to load.&lt;/li&gt;
&lt;/ul&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;ve run into similar problems and implemented a slightly different solution. Frustrated with searching for a utility to convert the CSV file, I eventually turned to the command line functions in MySQL. <a href="http://dev.mysql.com/doc/refman/5.0/en/load-data.html" target="_blank" rel="nofollow"><b>LOAD DATA INFILE</b></a> is the command to quickly load text file into a database table. </p>
<p>The easiest way to get to the command line client is to open MySQL Query Browser and choose <b>MySQL Command Line Client</b> from the <b>Tools</b> menu. You can also get there by typing <b>mysql</b> at a Windows command prompt; it is the same command in the Mac and Unix terminal. </p>
<p>Make sure you first select the database you want with the <b>use <i>database_name</i></b> command.</p>
<p>The syntax of <a href="http://dev.mysql.com/doc/refman/5.0/en/load-data.html" target="_blank" rel="nofollow"><b>LOAD DATA INFILE</b></a> can be a bit overwhelming at first. This is what I found worked best for CSV files exported from MS Access or phpMyAdmin.</p>
<p><code>LOAD DATA LOCAL INFILE 'c:/import.csv' INTO TABLE tbl_import_data FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' OPTIONALLY ENCLOSED BY '"' IGNORE 1 LINES (column_1, column_2, column_3, column_n);</code></p>
<p>There are many more options to this powerful command but the above example will handle the majority of your CSV needs. There are a few things to consider when using it:</p>
<ul style="list-style-type: circle;">
<li>Fields are usually terminated by a comma but could be a semi-colon, tilde, or whatever delimiter you like.</li>
<li>Specifying a line terminator may be necessary, depending on the text file format. This often depends on the operating system. Windows uses &#8216;\r\n&#8217; while Mac and Unix uses &#8216;\n&#8217;, which is the default.</li>
<li>Optional enclosing quotes are necessary when your text fields are enclosed by quotes, which is often the case by default. The &#8220;optional&#8221; keyword ensures that the command doesn&#8217;t die when it encounters a field that isn&#8217;t enclosed, such as an integer value.</li>
<li>Ignoring the first line is useful for when you export data with column names in the first row</li>
<li>Working on a local database may be easier, as special file permissions must be given to the user or the file must be accessible to the MySQL server.</li>
<li>When loading a local file, you must supply the full path of the file you want to load.</li>
</ul>
]]></content:encoded>
				</item>
	<item>
		<title>By: Wendy</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-6583</link>
		<author>Wendy</author>
		<pubDate>Wed, 23 Apr 2008 22:59:15 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-6583</guid>
					<description>But you are all using ENCLOSED BY '"' when Excel DOESN'T enclose the fields. How can any importer know what to do if a field has a comma in it? That will throw the field count off. The key is getting it OUT OF EXCEL with quotes. How do you do that?

Currently, I import the Excel file into Access and then export that in CSV (which allows you to select " to enclose the fields. But that causes problems too - especially with date fields.


/Wendy</description>
		<content:encoded><![CDATA[<p>But you are all using ENCLOSED BY &#8216;&#8221;&#8216; when Excel DOESN&#8217;T enclose the fields. How can any importer know what to do if a field has a comma in it? That will throw the field count off. The key is getting it OUT OF EXCEL with quotes. How do you do that?</p>
<p>Currently, I import the Excel file into Access and then export that in CSV (which allows you to select &#8221; to enclose the fields. But that causes problems too - especially with date fields.</p>
<p>/Wendy</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Mathieu</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-6862</link>
		<author>Mathieu</author>
		<pubDate>Fri, 02 May 2008 03:25:33 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-6862</guid>
					<description>I'm so new in that... but anyway...
OpenOffice (at least NeoOffice, the aqua-themed counterpart for Mac) DOES let you choose wether or not having fields enclosed with " ". 
It's free. Install it. Open a .xls file. Save it in the .csv formal. A window with many options will open. It's there.
I hope that can be usefull!</description>
		<content:encoded><![CDATA[<p>I&#8217;m so new in that&#8230; but anyway&#8230;<br />
OpenOffice (at least NeoOffice, the aqua-themed counterpart for Mac) DOES let you choose wether or not having fields enclosed with &#8221; &#8220;.<br />
It&#8217;s free. Install it. Open a .xls file. Save it in the .csv formal. A window with many options will open. It&#8217;s there.<br />
I hope that can be usefull!</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Mathieu</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-6864</link>
		<author>Mathieu</author>
		<pubDate>Fri, 02 May 2008 04:33:49 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-6864</guid>
					<description>i tried noah's clue through terminal.
It works! so well!
for someone who uses a client (i use CocoaMySQL -- yes, I'm a Mac), it's possible to type those commands directly in the "Custom Query" area. No use to mess with the terminal. Maybe everyone would have known this but I didn't!</description>
		<content:encoded><![CDATA[<p>i tried noah&#8217;s clue through terminal.<br />
It works! so well!<br />
for someone who uses a client (i use CocoaMySQL &#8212; yes, I&#8217;m a Mac), it&#8217;s possible to type those commands directly in the &#8220;Custom Query&#8221; area. No use to mess with the terminal. Maybe everyone would have known this but I didn&#8217;t!</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: JRSquared</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-7027</link>
		<author>JRSquared</author>
		<pubDate>Mon, 05 May 2008 18:37:44 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-7027</guid>
					<description>Is there a way to have this run automatically?  Say 1AM everyday?</description>
		<content:encoded><![CDATA[<p>Is there a way to have this run automatically?  Say 1AM everyday?</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: JRSquared</title>
		<link>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-7029</link>
		<author>JRSquared</author>
		<pubDate>Mon, 05 May 2008 19:56:12 +0000</pubDate>
		<guid>http://www.rtraction.com/blog/devit/excel-csv-to-mysql-hell.html#comment-7029</guid>
					<description>nevermind, I got JCron to work it out for me..</description>
		<content:encoded><![CDATA[<p>nevermind, I got JCron to work it out for me..</p>
]]></content:encoded>
				</item>
</channel>
</rss>
