Android Question Trouble connecting to MySQL database on local server

martin mitch

Member
Licensed User
Hi

I am just starting out with my B4A development (although I was a developer many years ago in various different languages). I would like to connect to a MySQL database that is running on a local server (my laptop, to begin with).

I have my PHP server running at 127.0.0.1:3306 (localhost) and if I run some PHP, I can connect and read data from a 'Tester' database with a table called 'Name' in it.

I would like to see if I can get an Android app to do the same thing.

I found @Star-Dust's excellent post here here which I am trying to modify to get it to work for me.

The code compiles and executes, but it does not connect to my database, can anyone point me in the right direction? I know it's going to be something silly, but what am I doing wrong?

Thanks

B4X:
#Region  Project Attributes
    #ApplicationLabel: SQL
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'MySQL Connector/J Driver
#AdditionalJar: mysql-connector-java-5.1.47-bin.jar


#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim MYSQL As SD_SQL
    
    Private MyLocation As String = "127.0.0.1:3306"
    Private MyUsername As String = "root"
    Private MyPassword As String = "hd8sjdX!"

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private ListViewMy As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    

    Log("---------- MyDatabase (MySQL) ----------")
    MYSQL.Initialize(Me,"MYSQL", "com.mysql.jdbc.Driver", $"jdbc:mysql://${MyLocation}/Tester?useSSL=false"$,  MyUsername, MyPassword)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'Connected to MySQL Server
Sub MYSQL_Ready (Success As Boolean)
    ListViewMy.SingleLineLayout.Label.TextColor=Colors.Black
    ListViewMy.SingleLineLayout.Label.Textsize=13
    ListViewMy.Clear
    If Success Then
        Dim RS As SD_ResultSet = MYSQL.ExecQuery("SHOW TABLES")
        Do While RS.NextRow
            ListViewMy.AddSingleLine(RS.GetString2(0))
        Loop
        RS.Close
    End If
End Sub

'Connected to MS SQL Server
 

Kevin Hartin

Active Member
Licensed User
mysql by default does not allow remote access, you need to make some modes to the mysqld.cnf file before any remote device can connect to the mysql server.

you will also have to open up any firewall to allow 3306 to be connected to.

I experienced a similar situation a few weeks ago when I was trying to migrate mysql off my web server onto a standalone sql server.

Kev
 
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
Hi Mitch, since I'm also struggling with database issues, which person above had the solution for you. Maybe you can explain how and perhaps why it worked for you.
Thanks,
 
Upvote 0
Top