Sublime Text 2 – Execute MySQL Query

This is a little hack to allow you to execute your SQL scripts into your mysql client.

In Sublime Text 2. Go under Tools -> Build System -> New Build System.

Enter in the following.

{
	"cmd": ["mysql", "-u", "root", "-e", "source $file"],
	"selector": "source.sql"
}

tweak it however you need. Save it. Then just open up any sql file and use Ctrl+B to run the sql. Don’t forget to add the “use myCoolDataBaseName” at the top of your sql scripts.

About these ads

13 comments

  1. Thanks for this!

    I’m using TOAD right now to edit my SQL for Oracle, and I’d love a way to do the same in Sublime2. I know it wouldn’t be hard to make it execute the query against the server, but capturing the returned data and displaying it back to the user is beyond my abilities right now.

  2. hi, this is a rather old post but i still want to ask. once i set this up on my machine i get no such file or directory in the sublime console. any ideas? thanks! :)

    1. What OS are you using?

      Open a command prompt(Windows) or Terminal (Linux/OSX) and type “mysql”, then press enter. What result does that give you?

  3. If you are on Windows you have to concatenate the flag and the argument, like so:

    {
    “cmd”: ["mysql", "-uroot", "-pyourpassword", "-e", "source $file", "-v"],
    “selector”: “source.sql”
    }

    Thus: “-u”, “root” becomes “-uroot” and “-p”, “yourpassword” becomes “-pyourpassword”

  4. example for me :(windows 7 ) // @santiblanko(twitter)

    {
    “cmd”: ["C:\\wamp\\bin\\mysql\\mysql5.5.20\\bin\\mysql.exe", "-uroot", "", "-e", "source $file", "-v"],
    “selector”:”source.sql”
    }

  5. Just thought I’d point out that the code found in the comments won’t work. not because its incorrect, but because the way the comments are formatted, the quotes (” “) are actually not quotes, and are interpreted as different characters, so it will fail when trying to build.

    Follow the comments above, but make sure to correct the quotes if copy-pasting (this only applies to the comments, as the code in the code block of the author is fine). Sublime will actually point this out.

    Definitely works on windows. So much better having added control over the input. Thanks for the guide.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s