Warning: maybe a bit offtopic, since it’s more a howto-pipe-questions.
Task: I want to create a user and database for mysql using applescript. This has to be done in case the database or user doesn’t exist.
I thought that the easiest way sis to built a string that contains the necessary mysql-commands:
GRANT ALL PRIVILEGES ON database_name.* TO ‘username’@‘localhost’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON database_name.* TO ‘username’@‘%’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;
CREATE DATABASE database_name;
EXIT;
All these 4 commands should be in one line:
set mysql_command to line1 & line2 & line3 & line4
No I thought that I can use a do shell-command like:
mysql -u USERNAME --password=PASSWORD mysql | echo “mysql_command”
Got an idea?
Unfortunately this piping isn’t working, so back to the questions:
Howto use applescript to execute some MySQL-commands.
It would be pretty nice to check if the datatabase or user exist and only if not the above command will be executed.
Best regards
Phil
Model: iBook G3 800Mhz
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)
property mysAppPath : "/usr/local/mysql/bin/mysql"
property mysUser : "root"
property mysPass : "rootpassword"
property mysHostAddr : "localhost" --"11.22.33.44"
property mysDbName : "DatabaseName"
property mysCommand : mysAppPath & " -u " & mysUser & " -h" & mysHostAddr & " --skip-column-names " & mysDbName
on mqCommand(s)
-- return list if successful, empty string if not
if character -1 of s ≠";" then set s to s & ";"
set rslt to do shell script "echo \"" & s & "\"|" & mysCommand & " 2>&1;echo -n $?"
if character -1 of rslt ≠"0" then
return ""
end if
if rslt = "0" then
return {}
end if
set rslt to text 1 through -3 of rslt -- strip return & $?
set AppleScript's text item delimiters to ASCII character 13
set lst0 to text items of rslt
set lst1 to {}
set AppleScript's text item delimiters to tab
repeat with itm in lst0
set end of lst1 to text items of itm
end repeat
return lst1
end mqCommand
-- unifyList puts everything in the list at one level
on unifyList(l)
set lst to {}
repeat with itmL in l
repeat with itm in itmL
set end of lst to contents of itm
end repeat
end repeat
return lst
end unifyList
set rslt to mqCommand("select seqNum from theDatabase where theField='" & theValue & "' order by theField desc;")
set rslt to unifyList(rslt)
I’ve stripped this from a larger Xcode script, hopefully retaining all the relevant stuff. Just plug in any old SQL command as the mqCommand argument and it should work. For what it’s worth…
Unfortunately I think that this is way to much code or to be more honest, I’m to dumb to fully understand what’s going on here.
Since I’m need the ‘do-some-mysql-commands’-funtion only as a part in a bigger script and I fully want to understand what I am coding, I hope that there is an easier solution, but I’ll try if the above script will work.
Hidden in my mess of code is that solution, it’s just surrounded by a lot of stuff to make it easier to use (but harder to understand!) Anyhow, glad you found a more straightforward explanation!