Android Question MsMySQL - Unable to connect

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

My MySQL database is accessible from "outside". All my other applications (VB and PHP) are able to connect to this database. Unfortunately from B4A, it is saying communication failure and the app is not getting connected to the DB

Even the firewall is OFF on the PC running the Database. I am unable to identify the reason.

The version of the LIB is 1.09

Here is the code that I use, Please note that I am using a Service

B4X:
#Region  Service Attributes
    #StartAtBoot: False

#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    ' Database related Global Values
    Dim cDbIP As String :cDbIP="192.168.0.154"
    Dim cDataBase As String :cDataBase="MyDbName"
    Dim cDbUserName As String :cDbUserName="root"
    Dim cDbPassword As String :cDbPassword="password"

    Dim oCn As MySQL
End Sub

Sub Service_Create
    Log("Inside Service Create")

    Try
        oCn.Initialize("MyDB",cDbIP,cDbUserName,cDbPassword,cDataBase,False,True)
        oCn.EnableReconnect
        Log("Log : Connected")
    Catch
        Log("Log : Error could not connect")
    End Try

End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("Inside Service start")
    oCn.check_connection
End Sub

Sub Service_Destroy
    oCn.CloseDatabase
    Log("Inside Service Destroy")
End Sub

Sub ExecQuery(Parameters As Map)

    'This is how other Activity Modules call this Service
'CallSubDelayed2(DbService,"ExecQuery",CreateMap("Sql":cSql,"TaskID":"GetUsrName","CallBackModuleName":"Main","CallBackSubName":"MyFunction") )
    Log("Inside ExecQuery")
    Dim cSql As String    = Parameters.Get("Sql")
    Dim cTaskID As String = Parameters.Get("TaskID")

    Dim MyArray(2) As String
    MyArray(0)=Parameters.Get("CallBackModuleName")
    MyArray(1)=Parameters.Get("CallBackSubName")

    'Adding the Task ID to the List
    Starter.DbTasksList.Put(cTaskID,MyArray)
    oCn.QueryASync(cSql,cTaskID)
End Sub

Sub MyDB_QueryResult(Data As List, Meta As Map)
    Log("inside MyDB_QueryResult")

End Sub

Sub MyDB_Status(Connected As Boolean, ReConnecting As Boolean, RetriesLeft As Int)
   Log("Connected = " & Connected)
   Log("ReConnecting = " & ReConnecting)
   Log("RetriesLeft = " & RetriesLeft)
End Sub

This is waht it shows on my Log.

LogCat connected to: 83f0485a4b484c53
--------- beginning of main
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (dbservice) Create **
Inside Service Create
Can not connect to the MySQL Database Server. Please check your configuration.
Hostname: 192.168.0.154
Username: root
Error: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Raising.. mydb_status
not Connected to Database
Log : Connected
** Service (dbservice) Start **
Inside Service start
MySQL Database not connected!
Connected = false
ReConnecting = false
RetriesLeft = 0
Inside ExecQuery
MySQL Database not connected!
lib:Exception:java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
SqlException: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
inside MyDB_QueryResult
Main
DevExistOrNot
** Service (newinst2) Create **
---- AppUpdating.NewInst2: service created
** Service (newinst2) Start **
-- AppUpdating.NewInst2: processing service_start
** Activity (main) Pause, UserClosed = false **



Is there anything particularly that I should check ? This database is on a LAN. The firewall is disabled in the PC to ensure that it's not the firewall that is causing this issue. THis database is accessible from other PC's through HeidiSQL and other Applications. Wondering what is the cause.

Any help will be appreciated.

Regards
Anser
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Looks like the Host cannot be reached or does not respond.

Maybe you are using another port on the mysql server running at this ip. Defult is 3306

The Device running the App is in the same Network?
 
Last edited:
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Resolved.

Instead of
oCn.Initialize("MyDB",cDbIP,cDbUserName,cDbPassword,cDataBase,False,True)
IT should be
oCn.Initialize("MyDB",cDbIP,cDbUserName,cDbPassword,cDataBase,True,True)

ie the StrictMode should be True
 
Upvote 0
Top