tags:

views:

115

answers:

2

How can I make SQL queries from within emacs scripts to MySql then print the result set?

#!/usr/bin/emacs --script

(setq sql "select id, name
from foobar
order by name
")

(princ sql)
+3  A: 

These may assist you:

http://www.emacswiki.org/emacs/SqlMode

http://atomized.org/2008/10/enhancing-emacs%E2%80%99-sql-mode/

Paul Nathan
This simply starts a MySql subprocess in a buffer then keys are bound to send sql statements to the process in the other buffer. It's almost like working from MySql from a shell.
+2  A: 

If you need a really reliable way to use mysql I would suggest using python mysql libraries via pymacs.

If you need it just working use mysql comand line client? Mysql is quite easy to use in scripts. Check out the example below

$ mysql -u root -p<password> -B <db> --execute="select * from projects"

p_id    p_timestamp     p_name  p_manager_id    p_status
1       2009-04-14 14:42:00     Test1   3       1
2       2009-04-14 14:42:13     Test2   3       1

The output should be easy to interpret as it looks like tab separated CSV. So it should be ok for most scenarios.

Piotr Czapla
Shell'ing out is inefficient but it will work. (split-string (shell-command-to-string "mysql -u root -p<password> -B <db> --execute='select id,name from projects'") )