Executing MySQL queries within VIM

RustyRazorblade Consulting RustyRazorblade Consulting
1 min read

I haven’t been using vim for very long, but I’ve gotten over the initial learning curve of getting used to the different editing modes. With some help from the guys in #vim on irc.freenode.net, I managed to get this gem:

` map :call SwitchDB() :function SwitchDB() : let g:current_db = input(“Database > “) :endfunction

map :call Doquery() :function Doquery() : if !exists(“g:current_db”) : call SwitchDB() : endif : let query_string = input(g:current_db . " > " ) : if query_string != "” : exe “!mysql " . g:current_db . " -e "” . escape(query_string, ‘”’) . “"” : endif :endfunction `

I haven’t been using vim for very long, but I’ve gotten over the initial learning curve of getting used to the different editing modes. With some help from the guys in #vim on irc.freenode.net, I managed to get this gem:

` map :call SwitchDB() :function SwitchDB() : let g:current_db = input(“Database > “) :endfunction

map :call Doquery() :function Doquery() : if !exists(“g:current_db”) : call SwitchDB() : endif : let query_string = input(g:current_db . " > " ) : if query_string != "” : exe “!mysql " . g:current_db . " -e "” . escape(query_string, ‘”’) . “"” : endif :endfunction `

Control-m to execute a query. Control-d to switch databases. It’ll prompt you the first time.

RustyRazorblade Consulting

RustyRazorblade Consulting

Apache Cassandra Consultant and Distributed Systems Expert

Related Posts

Need Expert Help with Apache Cassandra?

Get professional consulting for your distributed systems challenges. Performance optimization, architecture design, and troubleshooting.