<?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>Desi Penguin’s Blog &#187; sqlite3</title>
	<atom:link href="http://desipenguin.com/techblog/tag/sqlite3/feed/" rel="self" type="application/rss+xml" />
	<link>http://desipenguin.com/techblog</link>
	<description>Open source, Tech Tips, Reviews, Tutorials and more …</description>
	<lastBuildDate>Tue, 29 Jun 2010 16:38:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Importing CSV file data into sqlite3</title>
		<link>http://desipenguin.com/techblog/2009/07/13/importing-csv-file-data-into-sqlite3/</link>
		<comments>http://desipenguin.com/techblog/2009/07/13/importing-csv-file-data-into-sqlite3/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 15:41:16 +0000</pubDate>
		<dc:creator>Mandar Vaze</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Comma-separated values]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://desipenguin.com/techblog/?p=327</guid>
		<description><![CDATA[



Image via Wikipedia



I was trying to import the data from CSV file into sqlite3 database. Ideally this should be very simple task, with following the steps given in the sqlite tutorial. It is a matter of calling the sqlite command with separator argument, followed by an import operation, as listed below.

sqlite3 test.db  "create table [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignleft" style="width: 223px;">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png"><img title="The :en:SQLite logo as of 2007-12-15" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/SQLite_Logo_4.png/300px-SQLite_Logo_4.png" alt="The :en:SQLite logo as of 2007-12-15" width="213" height="63" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>I was trying to import the data from <a class="zem_slink" title="Comma-separated values" rel="wikipedia" href="http://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> file into <a class="zem_slink" title="SQLite" rel="homepage" href="http://www.sqlite.org/">sqlite3</a> database. Ideally this should be very simple task, with following the steps given in the sqlite tutorial. It is a matter of calling the sqlite command with separator argument, followed by an import operation, as listed below.</p>
<blockquote>
<pre>sqlite3 test.db  "create table t1 (t1key INTEGER PRIMARY KEY,data TEXT);"
sqlite3 -separator , test.db ".import some.csv t1"</pre>
</blockquote>
<p>Except that main attribute of my CSV file was that it could contain single records with embedded comma. I was hoping that sqlite3 would be smart enough to detect that the fields were enclosed within double quotes and then separate by comma. But I soon realized that only a code specifically dealing with CSV would know about this.  As we can see in the example above, the import is a generic code and as a user I listed comma as a separator.</p>
<p>My Data looked something like this :</p>
<blockquote>
<pre>"1","data1"
"2","data2,data3"</pre>
</blockquote>
<p>So like  any *nix geek would do, I tried  providing double quote <strong>and</strong> comma as a separator. To my surprise it worked very well. I though separator would take only single character, and I had provided two (three?). Anyway, important thing to remember is to escape the single quote with a <a class="zem_slink" title="Backslash" rel="wikipedia" href="http://en.wikipedia.org/wiki/Backslash">backslash</a> (I didn&#8217;t try it without the backslash, may be that would work too)</p>
<p>So here is the syntax that worked :</p>
<blockquote>
<pre>sqlite3 -separator \", test.db ".import mydata.csv mytbl"</pre>
</blockquote>
<p><em>Update : Turns out <a href="http://code.google.com/p/sqlite-manager/downloads/list" target="_blank">SQLite Manager</a> is much better solution after all.  It is an </em><em><a id="project_summary_link" style="text-decoration: none; color: #000000;" href="http://code.google.com/p/sqlite-manager/">Extension for Firefox and other apps to manage any sqlite database.</a></em><em> Not only it took care of above situation, it also handled empty cells as well where the command line failed with following error message : </em></p>
<blockquote>
<pre><em>line 4: expected 3 columns of data but found 2</em></pre>
</blockquote>
<p><em>Data with missing cells : Notice two successive commas :</em></p>
<blockquote>
<pre><em>"1","data1","data2"
"2","data3,data4","data5"
"3",,"data6"</em></pre>
</blockquote>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/037f00cf-f2be-47cc-89d4-863c249a4f99/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_c.png?x-id=037f00cf-f2be-47cc-89d4-863c249a4f99" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://desipenguin.com/techblog/2009/07/13/importing-csv-file-data-into-sqlite3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
