2

windows 10, emacs 26.1

I know that in org mode I can insert source code and execute them. Nice.

But what about sql:

Here in my.org file:

 #+name: some_query
   #+BEGIN_SRC sql
      SELECT * FROM invoice WHERE id IN (1,2,3)
   #+END_SRC

Is it possible to execute query direct in org mode and get result?

1
  • 3
    Yes: there is ob-sql.el and ob-sqlite.el. The latter is specific to sqlite but the former can handle different database engines. Read the beginning of ob-sql.el to see how to use it and check out also the SQL doc on Worg. – NickD Nov 29 '18 at 17:18
1

Yes. ob-sql is what I would use. Link to documentation with examples: https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-sql.html

You can specify your connection credentials directly as parameters of the source block:

#+begin_src sql :engine postgresql :dbhost HOSTNAME :dbuser USERNAME :dbpassword PASSWORD :database DBNAME
SELECT * FROM TABLE
LIMIT 100
#+end_src

The results are formatted as org-mode table in RESULTS block below after execution.

You could also configure the credentials under a header with (source):

 * some header that the sql source block will be in
  :PROPERTIES:
  :header-args+: :results table
  :header-args+: :engine mssql
  :header-args+: :dbhost 123.123.123.123
  :header-args+: :database database-name-a
  :header-args+: :dbuser username-a
  :header-args+: :dbpassword password-a
  :END:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.