Database Scripting the EASY way with mySQL and Applescript

Along the way, I have needed to connect through a script to a database and didn’t have any of the bridges or scripting additions installed that I had often read about. At first, I use to think that I absolutely had to have them before I could connect. But as usual, after spending long hours of searching and trial by error, I discovered that the path to implementing a database within my XCode projects was far easier than I imagined. So, to give something back to the community, I’ve created this cheat sheet to help those that need to know how to do it the first time. It is a fairly simple and complete way to access your tables and perform the basics (add, delete, update, and retrieve). I hope it helps some of you.

APPLESCRIPT (mySQL database) Retrieve, modify, delete, add
Visit http://dev.mysql.com/doc/world-setup/en/world-setup.html to download and install the example database & data or use your own.

NOTE: Since I am using phpMyAdmin on a remote server (where my site is stored) I created the database ‘world’ and added a new user called ‘uworld’ with a password of ‘worldu’. I then imported the file from the above site (world.sql) to create the three tables containing the example data. So far, I now have a new database called ‘world’ that is populated with three tables and plenty of example data that is accessible to the new user called ‘uworld’. Don’t forget to move the created PHP files to your server and check that the path is the correct one or change to suit.

RETRIEVAL OF DATA:
Next, I need to create the PHP file to “retrieve” the data that I want to display from my Applescript file.

PHP CODE: retrieve.php

Now I need to create the script that will call the PHP and then retrieve and display the data from the database.

APPLESCRIPT CODE for retrieve.php

set cityname to "Kabul"
set myURL to "http://YOURSERVERNAME/test/retrieve.php?cname=" & cityname & ""
set checkit to (do shell script "curl " & quoted form of myURL)
set wordcount to count words of checkit
if wordcount is equal to 3 then
	set countrycode to {word 1} of checkit as string
	set district to {word 2} of checkit as string
	set population to {word 3} of checkit as integer
else
	set countrycode to {word 1} of checkit as string
	set district1 to {word 2} of checkit as string
	set district2 to {word 3} of checkit as string
	set district to district1 & "-" & district2
	set population to {word 4} of checkit as integer
end if
display dialog "The city of " & cityname & " has a population of " & population & " and is located in the district of " & district & ". The country's short code is " & countrycode & ""

MODIFY DATA:
Since I now want to modify the data within a record of the table of the database, I will need to write a different PHP file to call.

PHP CODE: modify.php

And the script to update the data would be:

APPLESCRIPT CODE for modify.php

set cityname to "Kabul"
set popnumber to 1759990
set myURL to "http://YOURSERVERNAME/test/modify.php?nameofcity=" & cityname & "&newnumber=" & popnumber & ""
set urlMy to (do shell script "curl " & quoted form of myURL)

DELETE DATA:
Deleting is just as easy

PHP CODE: delete.php

APPLESCRIPT CODE for delete.php

set cityname to "Kabul"
set myURL to "http://YOURSERVERNAME/test/delete.php?nameofcity=" & cityname & ""
set urlMy to (do shell script "curl " & quoted form of myURL)

ADD DATA:
Easily add data records as well

PHP CODE: add.php

APPLESCRIPT CODE for add.php

set cname to "Kabul"
set ccode to "AFG"
set cdist to "Kabol"
set npop to "1780000"
set myURL to "http://YOURSERVERNAME/test/add.php?cityname=" & cname & "&countrycode=" & ccode & "&district=" & cdist & "&population=" & npop & ""
set checkit to (do shell script "curl " & quoted form of myURL)

Figuring out the rest should be fairly easy from here.
Chuck

Model: Dual 2.5 G5
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

You might be interested in this tutorial by Craig Smith on AppleScripting a mySQL Database

Thanks for the reply, but if it works, and it works well, then it ain’t broke. While I admit that Craig is certainly more into Applescript than I, and I respect his knowledge for sure but for what I do, the way I do it suffices. From any system I can type a just simple script to gain access to any of my databases for the files or data I have stored there. The post that I wrote was more for someone just trying to get it done without learning a great deal and is much, much, simpler. For beginners, like me, it works great.
Thanks,
Chuck

Chuck,

Very cool! I would only add one suggestion for a little security and maintainability.

Put your connection in a separate file and make a new file called constants. You can
put your constants in an area outside the public_html folder so no one can get to
your user name and password.

Excellent post!

Cheers,

Craig Williams

Connection:

Constants:

I am doing this until figuring how to to do it through Filemakers ODBC,

My question is it creates a new row in my Mysql 5 db, but none of the values are being set.

Does anyone have any suspicions why. I check the parameters being passed at least 20 times. Does GET have a limit on how many characters through CURL? Because I am trying to send more than 255 characters.

How would I do this with POST?

The only other thing that I could think of is that I am submitting values to TINYINT, INT, DATE & TIME mysql columns, maybe they need data sent without quotes?

Thank you,

Jeff

Well reading this post now is that you shouldn’t work this way. Applescript has an great build in xml-rpc client so if you know PHP you can make xml-rpc responses for applescript easily. Also you’ve tons of lack of security and also you opened your database wide open for the world for injections. If it’s for testing in your own private network you’re probably save behind your firewall at home but when going over the internet I recommend nobody to use one of these examples, nor the way they work.

With injection I mean when I search for “’ OR ‘1’ = '1” gives me the complete database when retrieving data. A user can’t be allowed to manipulate queries on a web server through sophisticated post data.

Yes, this database doesn’t contain data that is of valuable except to me & is backed up all the time.

Thanks for your input though it is appreciated.

Does anyone have any input to my prevous post to solve it, it is still much needed and appreciated.

Thank you for taking a look.

Jeff

I was talking about the lack of security in general and made a little example of what I can do. I also can delete, insert or update anything in your database I can even change the password if I want. More or less I can do what is allowed for user uworld.