Re: Rep : Querying a SQL database
Re: Rep : Querying a SQL database
- Subject: Re: Rep : Querying a SQL database
- From: Ron Bishop <email@hidden>
- Date: Tue, 29 Apr 2003 11:06:16 -0500
This might have been sent twice - sorry
Here's a little script example of Applescript running a shell script to
query a MySQL database and return the results. Yea, it's got problems,
but it's a quickie that works...
##################################
property newline : ASCII character 10
property tmpfile : "/tmp/execme"
set theShellScript to "#!/bin/sh" & newline & "/usr/local/bin/mysql
-uroot -padmin redletterstudiodb <<MYSQL_INPUT" & newline & "SELECT
DISTINCT CONCAT(con_firname,\" \",con_lasname) as names FROM Contacts;"
& newline & "MYSQL_INPUT"
do shell script "echo " & quoted form of theShellScript & " > " &
tmpfile
do shell script "chmod +x " & tmpfile
set theTextResult to do shell script tmpfile
{theTextResult}
##################################
It works by creating a temp file -"/tmp/execme" - that it executes. The
shell script is a command line argument. rather than use the "-e"
option it creates a 'here-document" within the "<<MYSQL_INPUT....
MYSQL_INPUT".
I am logging into the database "redletterstudiodb" as "root" with
password "admin" (I know, I know....but it's just for testing). The
query selects first name (con_firname )and last name (con_lasname) from
every record and concates them with the " " (escaped for Applescript)
and returns them as "names" which is returned as the Applescript
variable theTextResult - to be done with as whatever I will...
Here's an example of using the "-e" option and piping the values to get
your MySQL server uptime
##################################
property newline : ASCII character 10
property tmpfile : "/tmp/execme"
set theShellScript to "#!/bin/sh" & newline & "/usr/local/bin/mysql -e
STATUS | grep \"^Uptime\""
do shell script "echo " & quoted form of theShellScript & " > " &
tmpfile
do shell script "chmod +x " & tmpfile
set theTextResult to do shell script tmpfile
display dialog theTextResult
##################################
All of this is dependent on where your "mysql" command is located...
All this command line is from the "MySQL Cookbook" from O'Reilly. The
Applescript/shell script came from postings here from Chris Espinosa
and others.
I forgot what started this thread - I think it was syncing a FileMaker
database to a MySQL database. You might want to do that from within
FileMaker or there are some other products that do that (I think, check
www.VersionTracker.com).
Ron Bishop
On Tuesday, April 29, 2003, at 04:52 AM, Steve wrote:
On Tuesday, April 29, 2003, at 10:18 am, Jean-Baptiste LE STANG wrote:
You could try to edit the script file of this Applescript Studio app
(see the link below> that generates backup of MySQL database, you
might found what you're looking for.
I haven't been able to find any proper documentation on using
AppleScript to call Command Line calls to a mySQL database running on
another machine and then do something useful with the data. While your
example is good, it calls a script residing on the local machine and
does nothing with the response.
If someone knows of a document about using command line calls to do
mySQL queries, it might be the way forward for me.
Steve
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.