Page 1 of 1

Cant conect database on start-> but only some times?!

Posted: Sat May 24, 2014 1:03 pm
by Asha
Hi and thanks for any help! :D

I have a very strange issue, I have a stack that once you have defined a database it saves the id in a file, then in the next start up, if the id info exists in the file it connects directly to the mysql to get the info from database... all wonderful, but... some times, the major part, it cant get the info and drops a "Unable to connect to database", the strange thing is that other times it works!? and before I put the start up connection to database it were working always...

So many thanks!

Re: Cant conect database on start-> but only some times?!

Posted: Sat May 24, 2014 1:12 pm
by Klaus
Hi Asha,

when (openstack? opencard?) do you connect to the database?
Could you please post your script?

But maybe it is just the internet connection that is not always reliable,
because it does work most of the time?


Best

Klaus

Re: Cant conect database on start-> but only some times?!

Posted: Sat May 24, 2014 2:25 pm
by Asha
Hi Klaus, many thanks for you time, again :)

Is when I open the card, I have a PreOpenCard (I tried to put in a OpenCard with the same result) that calls to the script getDatabase:

Code: Select all

on getDatabase
   

      put URL ("file:" & imagePathGet()  & "/tabla.txt") into tableId
      put URL ("file:" & imagePathGet()  & "/user.txt") into vActualUser
     
      
      connectToDB
      
      
      
      put "SELECT * FROM " &  tableId & " where id=0" into tQuery
      put revDataFromQuery(comma,return,gConnID,tQuery) into tRecords  
      
      put 1 into vUser[1][2] 
      put 2 into vUser[2][2] 
      put 3 into vUser[3][2] 
      put 4 into vUser[4][2] 
      put 5 into vUser[5][2] 
      put 6 into vUser[6][2] 
      put 7 into vUser[7][2] 
      put 8 into vUser[8][2] 
      put 9 into vUser[9][2] 
      put 10 into vUser[10][2] 
      put item 8 of tRecords into vUser[1][1]
      put item 9 of tRecords into vUser[1][0] 
      put item 10 of tRecords into vUser[2][1]
      put item 11 of tRecords into vUser[2][0] 
      put item 12 of tRecords into vUser[3][1]
      put item 13 of tRecords into vUser[3][0] 
      put item 14 of tRecords into vUser[4][1]
      put item 15 of tRecords into vUser[4][0] 
      put item 16 of tRecords into vUser[5][1]
      put item 17 of tRecords into vUser[5][0] 
      put item 18 of tRecords into vUser[6][1]
      put item 19 of tRecords into vUser[6][0] 
      put item 20 of tRecords into vUser[7][1]
      put item 21 of tRecords into vUser[7][0] 
      put item 22 of tRecords into vUser[8][1]
      put item 23 of tRecords into vUser[8][0] 
      put item 24 of tRecords into vUser[9][1]
      put item 25 of tRecords into vUser[9][0] 
      put item 26 of tRecords into vUser[10][1]
      put item 27 of tRecords into vUser[10][0] 
 
     
      put 0 into vMany
      repeat with cVariable= 1 to 10
         if vUser[cVariable][1] > 0  then 
            add 1 to vMany
         end if
      end repeat
      
      
      
      # this part's not required, but a good idea to check for errors
      if tRecords begins with "revdberr" then 
         answer error "There was a problem accessing the database: of Users"
         closeDB gConnID
         exit to top
      end if
      # end error check
      --end if
      
      
        
end getDatabase
and this in the stack:

Code: Select all



on connectToDB
   put revOpenDatabase ("mysql","www.xxx.com”,”xx”,”xx”,”xx") into  tConxn

   # it's a good idea, but not required, to check for a successful connection here
   if tConxn is a number then
      put tConxn into gConnID
         --revExecuteSQL gConnID, "SET NAMES 'utf8'"

      return empty
   else
      put "Unable to connect to database"  into tErrMsg
      answer error tErrMsg as sheet
      put empty into gConnID
      return tErrMsg
      exit to top
   end if
end connectToDB


on closeDB pConnID
    revCloseDatabase pConnID
    
    # this ensures that you'll never have a left-over connection number in the global var
    if pConnID = gConnID then
        put empty into gConnID
    end if
end closeDB

And the internet connection goes on ADSL with no problem. :wink:

Re: Cant conect database on start-> but only some times?!

Posted: Sat May 24, 2014 2:38 pm
by Klaus
Hi Asha,

OK, I see.

Hm, sometimes the Livecode (database) libraries are not yet loaded "on pre-/opencard" so try to add a little delay like this:

Code: Select all

## Putting the connection here will only open that database ONCE!
## "pre-/opencard" maybe called more than once, so will teh db connect script!
on openstack
  send "getdatabase" to me in 100 millisecs 
  ## more openstack stuff here...
end openstack
At least worth a try :D


Best

Klaus

Re: Cant conect database on start-> but only some times?!

Posted: Sat May 24, 2014 3:39 pm
by Asha
Thanks Klaus!!!!

Your solution was not working, because just after the getDatabase it was using the info form database and it was not working right... but because your solution and explanation I tried a wait 110 milisec just before the getDatabase and now it works aaaaallllllwayssss :P

Thanks so much!

Re: Cant conect database on start-> but only some times?!

Posted: Sat May 24, 2014 3:47 pm
by Klaus
Hi Asha,

Okie Dokie :D


Best

Klaus