aardvark.code
 
04_sqlite
20160515

Sqlite example

What?

In following example aardvark stores one entry's content in the tag-dictionary, and extracts a 3 files from the aardvark.code file:

  • the create_load_exec.sql script
  • the aardvark.sh script (which gets auto-executed when aardvark finishes writing the files)
  • the data.csv file

The executed script pumps data into a sqlite database, and runs a sql query on it.

Prerequisite

For this example you need to have following software installed on your computer:

Go aardvark

Grab this aardvark.code file:

wget http://data.munging.ninja/aardvarkcode/sqlite/aardvark.code

The $key and value

Look at the code: when aardvark finds a 'filename' that starts with a dollar '$' (eg $sql), it is not considered a file but a key/value pair. The value (content) is stored in a dictionary under the key (eg. '$sql'). Further down in the 'aardvark.code' file, this content is pulled into a script by the identifier '[[$sql]]'.

Execute

Execute aardvark. After some housekeeping messages, you'll see the result from the query:

$ aardvark 
..
..
TNM SCRM    Magallanes & Antártica
SMB SCSB    Magallanes & Antártica
WPR SCFM    Magallanes & Antártica
PNT SCNT    Magallanes & Antártica
WPU SCGZ    Magallanes & Antártica
PUQ SCCI    Magallanes & Antártica

The aardvark.code file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
##== $sql =====================================================================
select iata,airport_name,region
from   t_airport
where  region like '%ca' ;
##=============================================================================
##== create_load_exec.sql -----------------------------------------------------
create table t_airport (
     iata varchar(8)
    ,icao varchar(8)
    ,city_served varchar(64)
    ,region varchar(64)
    ,airport_name varchar(128)
    );
.mode csv
.import data.csv t_airport 
.mode tabs
[[$sql]]
##=============================================================================
##== aardvark.sh --------------------------------------------------------------
#!/bin/bash 
rm test.db 
cat create_load_exec.sql | sqlite3 test.db  
##=============================================================================
##== data.csv -----------------------------------------------------------------
WAP,SCAP,Alto Palena,Los Lagos,SCAP
ZUD,SCAC,Ancud,Los Lagos,SCAC
TNM,SCRM,Antarctica,Magallanes & Antártica,SCRM
ANF,SCFA,Antofagasta,Antofagasta,SCFA
ARI,SCAR,Arica,Arica & Parinacota,SCAR
BBA,SCBA,Balmaceda,Aisén,SCBA
CJC,SCCF,Calama,Antofagasta,SCCF
WCA,SCST,Castro,Los Lagos,SCST
SMB,SCSB,Cerro Sombrero,Magallanes & Antártica,SCSB
WCH,SCTN,Chaitén,Los Lagos,SCTN
CNR,SCRA,Chañaral,Atacama,SCRA
CCH,SCCC,Chile Chico,Aisén,SCCC
YAI,SCCH,Chillán,Biobío,SCCH
GXQ,SCCY,Coihaique,Aisén,SCCY
LGR,SCHR,Cochrane,Aisén,SCHR
CCP,SCIE,Concepción,Biobío,SCIE
CPO,SCHA,Copiapó,Atacama,SCHA
COW,SCQB,Coquimbo,Coquimbo,SCQB
ZCQ,SCIC,Curicó,Maule,SCIC
ESR,SCES,El Salvador,Atacama,SCES
FFU,SCFT,Futaleufú,Los Lagos,SCFT
IQQ,SCDA,Iquique,Tarapacá,SCDA
IPC,SCIP,Isla de Pascua,Valparaíso,SCIP
LSC,SCSE,La Serena,Coquimbo,SCSE
ZLR,SCLN,Linares,Maule,SCLN
LOB,SCAN,Los Andes,Valparaíso,SCAN
LSQ,SCAG,Los Ángeles,Biobío,SCAG
ZOS,SCJO,Osorno,Los Lagos,SCJO
OVL,SCOV,Ovalle,Coquimbo,SCOV
WPR,SCFM,Porvenir,Magallanes & Antártica,SCFM
ZPC,SCPC,Pucón,Araucanía,SCPC
WPA,SCAS,Puerto Aisén,Aisén,SCAS
PMC,SCTE,Puerto Montt,Los Lagos,SCTE
PNT,SCNT,Puerto Natales,Magallanes & Antártica,SCNT
WPU,SCGZ,Puerto Williams,Magallanes & Antártica,SCGZ
PUQ,SCCI,Punta Arenas,Magallanes & Antártica,SCCI
QRC,SCRG,Rancagua,OHiggins,SCRG
SSD,SCSF,San Felipe,Valparaíso,SCSF
SCL,SCEL,Santiago,Santiago Metropolitan,SCEL
ULC,SCTI,Santiago,Santiago Metropolitan,SCTI
TLX,SCTL,Talca,Maule,SCTL
ZCO,SCTC,Temuco,Araucanía,SCTC
TOQ,SCBE,Tocopilla,Antofagasta,SCBE
ZAL,SCVD,Valdivia,Los Ríos,SCVD
VLR,SCLL,Vallenar,Atacama,SCLL
VAP,SCVA,Valparaíso,Valparaíso,SCVA
KNA,SCVM,Viña del Mar - Concón,Valparaíso,SCVM
 
Notes by Data Munging Ninja. Generated on akalumba:sync/20151223_datamungingninja/aardvarkcode at 2018-02-24 12:57