From 50fe1a6319dc13b41326702f6ea566f2241e7e52 Mon Sep 17 00:00:00 2001 From: Eric Abrahamsen Date: Thu, 7 Nov 2019 16:08:54 -0800 Subject: [PATCH] Add more examples to the ob-sqlite section * org-contrib/babel/languages/ob-doc-sqlite.org: Add tips on inserting new values into existing tables, and using an intermediary Org table to edit SQL tables. --- org-contrib/babel/languages/ob-doc-sqlite.org | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/org-contrib/babel/languages/ob-doc-sqlite.org b/org-contrib/babel/languages/ob-doc-sqlite.org index 2dafa1ab..0512c9a4 100644 --- a/org-contrib/babel/languages/ob-doc-sqlite.org +++ b/org-contrib/babel/languages/ob-doc-sqlite.org @@ -212,4 +212,44 @@ select n, count(*) from testtable group by n; | 10 | 2 | #+END_EXAMPLE +If dropping/overwriting a table is undesirable, a temporary SQL table +can be used to insert new values into an existing table: +#+BEGIN_EXAMPLE +,#+begin_src sqlite :db /tmp/rip.db :var orgtable=tableexample :colnames yes +create temporary table temp_table(id int, n int); +.mode csv testtable +.import $orgtable temp_table +insert into existing_table (id, n) select id,n from temp_table; +,#+end_src +#+END_EXAMPLE +** Using Org tables as an updatable "view" on SQLite tables +Org tables can be used to conveniently display some data from a SQLite +table, allow the user to edit it in Org, and the re-insert the updated +data into the underlying SQLite table. Do this by naming the results +table, then using it as input to another SQLite block that updates +rows. If your table has a primary key, you'll definitely want to use +it to make sure the correct rows are edited. + +#+BEGIN_EXAMPLE +,#+begin_src sqlite :db /tmp/reviews.db +select id,title,rating from bookreview where rating is null; +,#+end_src + +,#+name: ratings +,#+RESULTS: +| 5 | To Kill a Mockingbird | null | +| 12 | Three Body Problem | null | + +,#+begin_src sqlite :db /tmp/reviews.db :var ratings=ratings +create temporary table updates (id, title, rating); +.mode csv updates +.import $ratings updates +update bookreview set rating = (select rating from updates +where bookreview.id = updates.id) +where exists (select * from updates where updates.id = bookreview.id); +#+END_EXAMPLE + +By editing the intermediary table to replace "null" values with a +numerical rating, and then running the second source block, the SQLite +table will be updated correctly. -- 2.24.0