Android Question MSMariaDB/MSMySQL check if database is connected and try again if not!

Magma

Expert
Licensed User
Longtime User
Hi there i was reading this thread about check_connection but i can't find the point i want.

When the tablet/phone is disconnected from wifi and want to connects at db ofcourse can't connected... I am having a label that saying is not connected at internet but i wanna app try every 15 seconds to get connected so i believed that i must use the following:

Somewhere read that check_connection is like a ping and if not connected at the db will try again..

B4X:
Sub Timer2_Tick
    db.check_connection
end sub

but getting this:

What's the best solution to try to connect again to the db if not connected from the start (because device was not connected to the internet) ?
 

Magma

Expert
Licensed User
Longtime User
I ve found a solution (i think is not the best because at start always trying - not know if will make that a problem)...

So now if someone open app and server is down or wifi/3g/4g off - app will always try until connected...

If "data" enabled (2g/3g/4g/wifi) or server is up app will connected to db and do the job..

I don;t know if the following code will always work or will give an error or something... anyone can help optimize it ?


B4X:
Sub Activity_Create(FirstTime As Boolean)

'i know i can use firsttime - this is an example so don't use all  space of community
SERVERON=False
'not connected yet...
    connect2db
end sub

Sub connect2db
    db.DisableReconnect
'let's try
    db.Initialize("MySQL","myipormyhost:myport","myusername","mypass","mydatabase") 
   
End Sub

Sub MySQL_Status(Connected As Boolean, ReConnecting As Boolean, RetriesLeft As Int)
    Log($"Connected = ${Connected}"$)
    If Connected = True Then
'if connected then open table...
        db.QueryASync("select.....blahblah;","nameofjob")
'enable reconnect!!!!
        db.EnableReconnect
        SERVERON=True
        Else
'try reconnection
        SERVERON=False
        connect2db
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…