top | item 41619268

(no title)

sinker | 1 year ago

I've started using Emacs as a database explorer.

So, Emacs has a built-in interactive SQL mode (M-x sql-mysql / postgres / sqlite). This mode opens a SQL shell similar to what you would see in a terminal. From there, you can do your selects, inserts, updates, etc.

You can also send strings from a different buffer to your SQL shell buffer.

Now in Emacs you can very easily evaluate Lisp code to define functions, redefine functions, and execute arbitrary expressions. You can also wrap your SQL expressions inside of Lisp code. By doing so, you can take advantage of Emacs's built-in Lisp evaluation tools to interact with your SQL database.

So instead of opening a shell in your terminal, selecting a database, and writing select statements to inspect your DB, you can instead...

In Emacs, create a file called something like "sql-notebook.el". Inside that file you write Lisp expressions to execute SQL queries. To execute those queries, you move the cursor over it and just run the command `eval-last-expr` (I have this bound to Ctrl-c Ctrl-c). The results of the evaluated expressions appear in your interactive SQL buffer.

The obvious advantage of this is that you end up creating a library of often-used queries which are very convenient to execute simply by moving the cursor over the query and hitting Ctrl-c twice.

You also retain a history of these queries by virtue of them existing in a plain file, as opposed to ephemeral shell history.

discuss

order

maverick98|1 year ago

hey thats pretty cool, I will try to see how that works, I didn't know it. But it sounds like a lot of work to set it up. I wanted to make something faster

sinker|1 year ago

It's not a lot of work, but it takes a little bit of existing Elisp knowledge. You can still evaluate plain SQL code in a buffer (sql-mode) and get most of the benefits I described, you just won't get the convenience of evaluating Lisp forms.