Combining 2 scripts

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ronniebellie
Posts: 28
Joined: Fri Jun 26, 2015 6:17 pm

Combining 2 scripts

Post by ronniebellie » Tue Jul 14, 2015 4:48 pm

I'm working with getting info from a MySQL to show up in a card. That part is working. However, I have 2 separate scripts. One to open the database and one to put the data into a field. Can these two below be combined? The first one is the opening the database. The second one is to put the data into a field.

on mouseUp
    -- 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 "mysql" into theDBType
   put "XXXXXXXXXXX" into theDBHost
   put "XXXXXXXXXXX" into theDBName
   put "XXXXXXXXXXX" into theDBUser
   put "XXXXXXXXXXX" into theDBPassword
   put revOpenDatabase( theDBType, theDBHost, theDBName, theDBUser, theDBPassword ) 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 then
        put tResult into gConnectionID
    else
        put empty into gConnectionID
        answer error "Unable to connect to the database:" & cr & tResult
     end if
     go to card 4
end mouseUp

on mouseUp
    -- check the global connection ID to make sure we have a database connection
    global gConnectionID
    if gConnectionID is not a number then
        answer error "Please connect to the database first."
        exit to top
    end if
    
    -- construct the SQL (this selects all the data from the specified table)
    put "content" into tTableName    -- set this to the name of a table in your database
    put "SELECT content_body FROM content WHERE `content_name` = 'schedule'" into tSQL
    
    -- query the database
    put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData
    
    -- check the result and display the data or an error message
    if item 1 of tData = "revdberr" then
        answer error "There was a problem querying the database:" & cr & tData
    else
        set the HTMLText of field "schedule" to tData
    end if
end mouseUp

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Combining 2 scripts

Post by Klaus » Tue Jul 14, 2015 6:33 pm

Hi Ron,

like this:

Code: Select all

on mouseUp 
   -- 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 "mysql" into theDBType 
   put "XXXXXXXXXXX" into theDBHost 
   put "XXXXXXXXXXX" into theDBName 
   put "XXXXXXXXXXX" into theDBUser 
   put "XXXXXXXXXXX" into theDBPassword 
   put revOpenDatabase( theDBType, theDBHost, theDBName, theDBUser, theDBPassword ) 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 then 
      put tResult into gConnectionID 
   else 
      put empty into gConnectionID 
      answer error "Unable to connect to the database:" & cr & tResult 
      exit to top
   end if 
   
   -- construct the SQL (this selects all the data from the specified table) 
   put "content" into tTableName    -- set this to the name of a table in your database
   put "SELECT content_body FROM content WHERE `content_name` = 'schedule'" into tSQL
       
   -- query the database
   put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData
       
   -- check the result and display the data or an error message
   if item 1 of tData = "revdberr" then
      answer error "There was a problem querying the database:" & cr & tData
      exit to top
   else

     ## I presume that this field is on Card 4, right?
      set the HTMLText of field "schedule" OF CD 4 to tData
   end if
   go to card 4 
end mouseUp 
Best

Klaus

ronniebellie
Posts: 28
Joined: Fri Jun 26, 2015 6:17 pm

Re: Combining 2 scripts

Post by ronniebellie » Wed Jul 15, 2015 7:08 pm

Thank you very much! That worked.

Post Reply

Return to “Databases”