AppleScript with MySQL

Anyone know if you can write MySQL statements with AppleScript? Do you need a special scripting addition?

Thanks,
Dave

I don’t know a scripting adidition but, on mac os 9 you have a stand alone app called MacSQL http://www.rtlabs.com, is scriptable.

On mac os X you have more options:

  • you can execute php code from the terminal, using the terminal app or directly from A.S. with “do shell script”.

  • you can buy a framework (for proyect builder (A.S. studio)) from the same company of MacSQL.

  • you can use a open source framework (for proyect builder (A.S. studio)) called SMySQL (search in http://www.sourceforge.net).

  • you can use a PHP in a remote machine and use “curl” command on terminal or “do shell script”.

-there is too a MacSQL app for mac os x, is a scriptable stand alone app.

I think maybe there are more ways of do this…

César Escolar

In Mac OS X, this works for me:

set mysql_user to "mysql_username"
set mysql_host to "mysql_host"
set mysql_pw to "mysql_password"
set mysql_db to "mysql_database"
set mysql_table to "mysql_table"
set mysql_key_field to "mysql_key_field"
set mysql_key_data to "mysql_key_data"
set sql_query to "SELECT * FROM " & mysql_table & " WHERE " & mysql_key_field & " = '" & mysql_key_data & "'"

set the_data to (do shell script "/usr/local/bin/mysql -D " & mysql_db & " -u " & mysql_user & " -h " & mysql_host & " -p" & mysql_pw & " -e "" & sql_query & """)

Of course, update the variables with the appropriate info for your DB.

Jon

If jonn8’s code is used, and the request takes some time then the password is visible to any other user connected to that Macintosh via telnet, ssh etc during that time.

Is there any way to get around this using the command line version of mysql from AppleScript.

I want a general solution for mysql, so that I can create databases and tables, which means that I will need more than can be achieved by creating a user that does not have a password and only has select privileges on selected databases.

Kevin