48 lines
1.5 KiB
HTML
48 lines
1.5 KiB
HTML
---
|
|
layout: post
|
|
title: "csql: perform sql queries on csv data"
|
|
redirect_from:
|
|
- /01318779088
|
|
---
|
|
<body>
|
|
|
|
The websites I operate are flat-file based, so when I want to store
|
|
data it's easiest to write it in csv format. But it's not particularly easy
|
|
to make queries on this data without additional software.
|
|
|
|
<p>I'd rather not fire up Open Office or try to figure out how to make awk
|
|
parse a quoted excel-format file, so I wrote 'csql', which reads a csv
|
|
file into an in-memory sql database, performs a query, and then writes
|
|
the result also in csv format.
|
|
|
|
<p>A short example:
|
|
|
|
<p>rsvp.csv:
|
|
<blockquote><pre>
|
|
Buffy Summers,false,0
|
|
Xander Harris,true,2
|
|
Willow Rosenberg,true,1
|
|
</pre></blockquote>
|
|
|
|
<p>rsvp.cols:
|
|
<blockquote><pre>
|
|
name text
|
|
attend boolean
|
|
count integer
|
|
</pre></blockquote>
|
|
|
|
<p>Performing queries:
|
|
<blockquote><pre>
|
|
$ csql rsvp.csv 'select sum(count) from t where attend="true"'
|
|
3
|
|
$ csql rsvp.csv 'select name from t where attend="true"'
|
|
Xander Harris
|
|
Willow Rosenberg
|
|
</pre></blockquote>
|
|
|
|
<p><p><b>Files currently attached to this page:</b>
|
|
<table cellpadding=5 style="width:auto!important; clear:none!important"><col><col style="text-align: right"><tr bgcolor=#eeeeee><td><a href="https://media.unpythonic.net/emergent-files/01318779088/csql">csql</a></td><td>3.4kB</td></tr></table><p>
|
|
|
|
<p><br><br><font size=-2>Entry first conceived on 16 October 2011, 15:31 UTC, last modified on 15 January 2012, 3:46 UTC</font>
|
|
</body>
|
|
</html>
|