<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://www.rupinderdhariwal.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.rupinderdhariwal.com/" rel="alternate" type="text/html" /><updated>2022-08-24T14:26:30+00:00</updated><id>https://www.rupinderdhariwal.com/feed.xml</id><title type="html">Rupinder Dhariwal</title><subtitle>A personal blog</subtitle><entry><title type="html">How to query your csv files using sqlite</title><link href="https://www.rupinderdhariwal.com/csvToSql/" rel="alternate" type="text/html" title="How to query your csv files using sqlite" /><published>2018-07-19T00:00:00+00:00</published><updated>2018-07-19T00:00:00+00:00</updated><id>https://www.rupinderdhariwal.com/csvToSql</id><content type="html" xml:base="https://www.rupinderdhariwal.com/csvToSql/">&lt;h3 id=&quot;prerequistes&quot;&gt;Prerequistes&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Make sure Sqlite3 is already installed
    &lt;ul&gt;
      &lt;li&gt;for linux: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt-get install sqlite3 libsqlite3-dev&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;for mac: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;brew install sqlite3&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;start sqllite3 shell by running
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;
sqlite3
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.mode&lt;/code&gt; command as follows to set it to csv:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.mode csv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.import FILE TABLE&lt;/code&gt; command to import the data from the city.csv file into the cities table.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/rdhariwal/csvTosqlQ&quot;&gt;Clone my repo for csv files&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.import city.csv cities
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If the table does not previously exist, the first row of the CSV file is interpreted to be column names and the actual data starts on the second row of the CSV file.&lt;/p&gt;

&lt;p&gt;To verify the import, you use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.schema&lt;/code&gt; command to display the structure of the cities table.&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.schema cities
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Query against the imported table by:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT name, population FROM cities;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;See if you can spot the problem between these two group of queries (hint: is population order by working?):&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT name, population FROM cities order by population desc;
SELECT name, population FROM cities order by population asc;

SELECT name, population FROM cities order by name asc;
SELECT name, population FROM cities order by name desc;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Drop table by:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;DROP TABLE cities;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create cities table again table:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CREATE TABLE cities (
  name Varchar,
  population Int
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Import csv file again but now in an existing table with population as Int and no headers (if table already exists then first row of csv becomes first data row of the table)&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.import city_no_headers.csv cities
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Try these queries again. because the table existed with population as Int the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order by&lt;/code&gt; behaviour is correct:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT name, population FROM cities order by population desc;
SELECT name, population FROM cities order by population asc;

SELECT name, population FROM cities order by name asc;
SELECT name, population FROM cities order by name desc;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For more sql syntax see documentation &lt;a href=&quot;https://www.w3schools.com/sql/sql_syntax.asp&quot;&gt;here&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><summary type="html">Prerequistes Make sure Sqlite3 is already installed for linux: sudo apt-get install sqlite3 libsqlite3-dev for mac: brew install sqlite3</summary></entry></feed>