سؤال

I have your standard hosted MySQL database (HostGator) and I want to connect to it from my Mac Laptop via Apple Script. Is this even possible? If so where can I find an example? The examples I have found are for connecting to a local MySQL instance.

I want to select from a table and make updates to a table.

Thanks

هل كانت مفيدة؟

المحلول

You could put a PHP script on your server that parses the SQL syntax. A very simple example would be

<?php
  include 'connect_to_database.php';
  $sql = $_GET['s'];
  $result = mysql_qery($sql);
  if (!result) echo mysql_error();
  else echo $result;
  include 'close_database.php';
?>

where the includes contain your database credentials.

Now you can use AppleScript. Suppose that the URL of your PHP file is http://www.exampledomain123.com/file.php then you can use this AppleScript:

sql = urlEncode("SELECT * FROM db_name;")
set shellScript to "curl \"http://www.exampledomain123.com/file.php?s=" & sql & "\""
do shell script shellScript

This should execute the required SQL syntax. It is recommendable to adjust this code by obfuscating the SQL syntax with an additional AppleScript routine and decode the obfuscated string in PHP.

You can find urlEncode routine for AppleScript at http://harvey.nu/applescript_url_encode_routine.html

نصائح أخرى

Here another approach, which uses shell scripting only and should work with a local MySQL installation:

set shellScript to "/usr/local/mysql/bin/mysql -u USER -pPASSWORD -D DATABASE -e \"SELECT * FROM mytable\""
do shell script shellScript
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top