mysql commands - add - search - update

I was wondering if any of you has ever written a script that allows applescript to talk to a mysql database and perform the following actions:

add - search - update

I know of this one:

http://scriptbuilders.net/files/mysql4applescript0.3b.html

yep
but you need to make sure you have the command mysql somewhere.


set DB to "car"
set Query to "select sum(Business)as Business , sum(Private)as Private from log;"
set myResult to doQuery(Query, DB)

on doQuery(Query, DataBase)
	set SQL_User to "-u root"
	set SQL_Pass to "-p" & "hello"
	set SQL_Command to "/sw/bin/mysql -B" -- B is for batch mode tab delimited
	set sp to " "
	do shell script "echo '" & Query & "' |" & sp & SQL_Command & sp & SQL_User & sp & SQL_Pass & sp & DataBase
end doQuery

can I use PHP instead ? it seems easier since it comes with os x and I just need to enable it in the http.conf file. the syntax seems also easier in PHP. what are your thoughts on it ?

This may help: PHP CLI

Hi Nyl,

I’ve also used the Script library that Bruce mentioned and then eventually changed my script to be self-contained.

The way is to run shell commands:


set theResult to do shell script "/usr/local/mysql/bin/mysql -u root -D 'itunes' -N -e \"insert into databasename " & queryString & "\"")
if theResult = "" then
   successful
end if

To get values from MySQL:


set queryResultsRaw to do shell script "/usr/local/mysql/bin/mysql -u root -D 'databasename'  -e \"SELECT " & "field1, field2" & " FROM databasename " & "WHERE ..." & "\""
set oldDelim to text item delimiters
set text item delimiters to {"\t"}
set queryResults to the text items of paragraph 2 of queryResultsRaw
set text item delimiters to oldDelim
set value1 to item 1 of queryResults
set value2 to item 2 of queryResults

Gary