3

I want to execute an SQL query and get its result in elisp:

(let ((results (do-sql-query "SELECT * FROM a_table")))
     (do-something-with results))

I'm using Postgres, and I already know all of my connection information (host, username, password, db et al) I just want to execute the query and get the result back, synchronously.

3 Answers 3

3

I haven't tried it, but pg.el looks promising.

Sign up to request clarification or add additional context in comments.

3 Comments

This is beautiful; I re-wrote my existing command using this. It's a bit slower, but it's a lot easier to work with.
The link is dead. I edited the answer to use github.com/cbbrowne/pg.el
Current location which includes many updates: github.com/emarsden/pg-el
0

I ended up calling out to psql using this form:

(shell-command-to-string
   (concat sql-postgres-program
                 " -h DBHOST -t"
                 " -c \"SELECT db FROM defines.databases WHERE role = '" tier "' AND db ~ '^" string "';\""
                 " -U USERNAME DBNAME"))

It seems to have worked all right. I have to chomp it (using the function Paul linked to) and split the results, but I get what I need out of it.

Comments

0

sql.el is built in to emacs and has two modes; you should be able to work with that to send mysql commands from elisp but it doesn't seem to make it obvious how you do it.

I'm not sure this works entirely from elisp without user interaction, or being in sql-mode. It does have functions to send a string to execute.

This link looks interesting; it piggy backs off mysql mode and makes the mode more like how you want it to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.