Tip of the day: Connect to MySQL and run a query
#Start a new connection
Set Variable [$Connection; Value:MBS("SQL.NewConnection")]
#Tell plugin where MySQL library is (put it where you like)
Set Variable [$result; Value:MBS("SQL.SetConnectionOption"; $Connection; "MYSQL.LIBS"; "/Users/cs/Desktop/libmysqlclient.dylib")]
#Connect to a mysql database:
Set Variable [$result; Value:MBS("SQL.Connect"; $Connection; "192.168.11.51@Server_Config"; "user"; "password"; "MySQL")]
If [$result ≠ "OK"]
#Connection failed
Show Custom Dialog ["Error: " & $result]
Set Variable [$result; Value:MBS("SQL.FreeConnection"; $Connection)]
Halt Script
Else
#Create a query:
Set Variable [$Command; Value:MBS("SQL.NewCommand"; $Connection; "SELECT * FROM Server_Config where ServerName=:Name")]
#If you use parameters, you can fill them here
Set Variable [$r; Value:MBS("SQL.SetParamAsText"; $Command; "Name"; "MacMini")]
#Execute it
Set Variable [$result; Value:MBS("SQL.Execute"; $Command)]
If [$result ≠ "OK"]
Set Field [MySQL Query::Result; $result]
Show Custom Dialog ["Error: " & $result]
Else
Set Variable [$lines; Value:""]
Set Variable [$fieldcount; Value:MBS("SQL.FieldCount"; $command)]
Loop
#call FetchNext to get the next record
Set Variable [$result; Value:MBS("SQL.FetchNext"; $Command)]
Exit Loop If [$result ≠ 1]
Set Variable [$line; Value:""]
Set Variable [$i; Value:1]
Loop
#We query field names and values to show them later
Set Variable [$v; Value:MBS("SQL.GetFieldAsText"; $command; $i)]
Set Variable [$n; Value:MBS("SQL.GetFieldName"; $command; $i)]
Set Variable [$line; Value:$line & $n & ": " & $v & ¶]
Set Variable [$i; Value:$i+1]
Exit Loop If [$i > $fieldCount]
End Loop
Set Variable [$lines; Value:$lines & ($line & ¶)]
End Loop
Set Variable [$lines; Value:$lines & ¶]
Show Custom Dialog ["Result from Query:"; $lines]
End If
#Cleanup
Set Variable [$result2; Value:MBS("SQL.FreeCommand"; $Command)]
End If
Set Variable [$result2; Value:MBS("SQL.FreeConnection"; $Connection)]