Tuesday, March 07, 2017

SQLite Source Code Blocks in Org Mode



SQLite Source Code Blocks in Org Mode--> http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-sqlite.html

# -*- coding: utf-8 -*-
#+STARTUP: content
#+LANGUAGE: ko
#+PROPERTY: header-args:sqlite :dir ~/org/ :db journal.db :var rel="cur" :results line :colnames yes

: header arguments -> :csv, :column, :line, :list, :html
: results -> silent raw
: separator \

※ 1. File scope variable which is name "#+PROPERTY" can be located any location of file.
※ 2. "#+PROPERTY" var will be initialize when file opening time.
    (therefore, if you modify "#+PROPERTY" it can be use until reopen the file again!)

* sqlite org babel

  [[http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-sqlite.html][guide_from orgmode.org]]

** emacs babel setup
#+TITLE: dot
#+begin_src elisp
(org-babel-do-load-languages
 'org-babel-load-languages (quote ((emacs-lisp . t)
                                    (sqlite . t)
                                    (R . t)
                                    (python . t))))
#+end_src

** Org Mode Features for SQLite Source Code Blocks
Header Arguments
Language-specific default values

There are no language-specific default header arguments for SQLite.

** Language-specific header arguments
   There are 11 SQLite-specific header arguments. 

db
    a string with the name of the file that holds the SQLite database. Babel requires this header argument.
header
    if present, turn on headers in the output format. Headers are also output with the header argument :colnames yes.
echo
    if present, set the SQLite dot command .echo to ON.
bail
    if present, set the SQLite dot command .bail to ON.
csv
    the default SQLite output format for Babel SQLite source code blocks.
column
    an SQLite output format that outputs a table-like form with whitespace between columns.
html
    an SQLite output format that outputs query results as simple HTML tables.
line
    an SQLite output format that outputs query results with one value per line.
list
    an SQLite output format that outputs query results with the separator character between fields.
separator
    a string that specifies the separator character used by the SQLite `list' output mode and by the SQLite dot command .import.
nullvalue
    a string to use in place of NULL values. 

** Variables
  It is possible to pass variables to sqlite. Variables can be of type table or scalar. Variables are defined using :var=<value> and referred in the code block as $<name>.

Table variables
    Table variables are exported as a temporary csv file that can then be imported by sqlite. The actual value of the variable is the name of temporary csv file.
Scalar variables
    This is a value that will replace references to variable's name. String variables should be quoted; otherwise they are considered a table variable.

** Sessions
  SQLite sessions are not supported.

** Result Types
   SQLite source code blocks typically return the results of a query. The header arguments :csv, :column, :line, :list, and :html determine the output format.

: #+PROPERTY: header-args:sqlite :dir ~/org/ :db org.db :results list


* sample create
: #+name: sqlite-populate-test
: #+header: :results line
: #+header: :dir ~/tmp/
: #+header: :db hello.db
: #+begin_src sqlite
: create table greeting(one varchar(10), two varchar(10));
: insert into greeting values('Hello', 'world!');
: #+end_src

#+begin_src sqlite
create table greeting(one varchar(10), two varchar(10));
insert into greeting values('Hello', 'world!');
#+end_src


* sample query
: #+name: sqlite-hello
: #+header: :list
: #+header: :separator \ 
: #+header: :results raw
: #+header: :dir ~/tmp/
: #+header: :db hello.db
: #+begin_src sqlite
: select * from greeting;
: #+end_src

#+begin_src sqlite
select * from greeting;
#+end_src

#+RESULTS:
| one   | two    |
|-------+--------|
| Hello | world! |


* Using scalar variables

** using local var.
#+BEGIN_SRC sqlite :db ~/org/journal.db :var rel="tname" n=300 :colnames yes
drop table if exists $rel;
create table $rel(n int, id int);
insert into $rel(n,id) values (1,210), (3,800);
select * from $rel where id > $n;
#+END_SRC

#+RESULTS:
| n |  id |
|---+-----|
| 3 | 800 |



** usign file var.
#+BEGIN_SRC sqlite :var n=300 
drop table if exists $rel;
create table $rel(n int, id int);
insert into $rel(n,id) values (1,210), (3,800);
select * from $rel where id > $n;
#+END_SRC

#+RESULTS:
| n |  id |
|---+-----|
| 3 | 800 |



* Using table variables

  We can also pass a table to a query. In this case, the contents of the table are exported as a csv file that can then be imported into a relation:


** local var.

#+NAME: tableexample
| id |  n |
|----+----|
|  1 |  5 |
|  2 |  9 |
|  3 | 10 |
|  4 |  9 |
|  5 | 10 |

#+begin_src sqlite :db ~/org/journal.db :var orgtable=tableexample :colnames yes
drop table if exists testtable;
create table testtable(id int, n int);
.mode csv testtable
.import $orgtable testtable
select n, count(*) from testtable group by n;
#+end_src

#+RESULTS:
|  n | count(*) |
|----+----------|
|  5 |        1 |
|  9 |        2 |
| 10 |        2 |


** using global file var.
#+begin_src sqlite :var orgtable=tableexample
drop table if exists testtable;
create table testtable(id int, n int);
.mode csv testtable
.import $orgtable testtable
select n, count(*) from testtable group by n;
#+end_src

#+RESULTS:
|  n | count(*) |
|----+----------|
|  5 |        1 |
|  9 |        2 |
| 10 |        2 |

No comments:

Pranten

 Pranten