Page 1 of 1

mySQL Examole and first steps in LiveCode

Posted: Sun Jul 27, 2014 10:22 am
by dx111ge
Good Morning,

please be aware i´m just started with my first test in Livecode and it seems to be very impressive. With the help of the examples i could connect to my own mysql Server from Mac and Windows. Making those working, i did try to change the code and add a disconnect. Basically if have two Buttons now : Connect and disconnect. See the code below. I thought the global gConnectionID would be available in the disconnect script, but it seems to be wrong. Maybe someone can shed some light on my dumb head ?
Thanks in advance

Code: Select all

  -- Connect Button
       -- use a global variable to hold the connection ID so other scripts can use it
       global gConnectionID
       
       -- set up the connection parameters - edit these to suit your database
       put "DBServer" into tDatabaseAddress
       put "test" into tDatabaseName
       put "xxxxxxx" into tDatabaseUser
       put "xxxxx" into tDatabasePassword
       
       -- connect to the database
        if gConnectionID is empty then put revOpenDatabase("MySQL", tDatabaseAddress, tDatabaseName, tDatabaseUser, tDatabasePassword) into tResult
       
       -- check if it worked and display an error message if it didn't
       -- & set the connection ID global
       if tResult is a number and gConnectionID is empty then
              put tResult into gConnectionID
              answer info "Connected to the database." & cr & "Connection ID = " & gConnectionID as sheet
    else if gConnectionID is not empty then
      answer info "Connected to the database." & cr & "Connection ID = " & gConnectionID as sheet
      else 
              put empty into gConnectionID
              answer error "Unable to connect to the database:" & cr & tResult 
       end if
   set the enabled of Button Disconnect to true
end mouseUp

Code: Select all

 -- Disconnect Button
on MouseUp
   answer info " ich schliesse " & gConnectionID as sheet
   revCloseDatabase gConnectionID
   
end MouseUp

Re: mySQL Examole and first steps in LiveCode

Posted: Sun Jul 27, 2014 10:58 am
by Peter Wood
As far as I remember, you need to declare the global variable in each script that you wish to use it. Try adding

Code: Select all

global gConnectionID
at the top of your Disconnect Button script

Re: mySQL Examole and first steps in LiveCode

Posted: Sun Jul 27, 2014 11:02 am
by dx111ge
Thank you very much , it works now !