Android Question Problem with syntax connecting Android to MySQL Database

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
If I refer at the tuto https://www.b4x.com/android/forum/threads/connect-android-to-mysql-database-tutorial.8339/ it seems it does not need "sql" library. Isn't it ?

I create on the server, as shown, a php script file, essai.php

Look at my code below, the Buttons-click : I do not find the exact syntax for:
ExecuteRemoteQuery("SELECT * FROM MyTable WHERE champs1 ='" & Val1 & "' ,ESSAI_3)
ExecuteRemoteQuery("SELECT COUNT MyTable ................,ESSAI_4)
Can you help me and indicate the good syntax? I find many samples but they do not run.

Thank you
B4X:
Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("https://myTable/applis/essai.php", Query)
End Sub

Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
            Case ESSAI_1
                Dim ESSAI1 As List
                ESSAI1 = parser.NextArray 'returns a list with maps
                For i = 0 To ESSAI1.Size - 1
                    Dim m As Map
                    m = ESSAI1.Get(i)
                    Dim valeur1, valeur2, valeur3 As string
                    valeur1 = m.Get("champs1")
                    valeur2 = m.Get("champs2")
                    valeur3 = m.Get("champs3")
                    Log(valeur1 & " " & valeur2 & " " & valeur3)
                Next
            Case ESSAI_2
                MsgboxAsync("It runs fine", "Result")

            Case ESSAI_3
                Dim ESSAI3 As List
                ESSAI3 = parser.NextArray 'returns a list with maps
                For i = 0 To ESSAI3.Size - 1
                    Dim m As Map
                    m = ESSAI3.Get(i)
                    Dim valeur1, valeur2, valeur3 As string
                    valeur1 = m.Get("champs1")
                    valeur2 = m.Get("champs2")
                    valeur3 = m.Get("champs3")
                    Log(valeur1 & " " & valeur2 & " " & valeur3)
                Next

            Case ESSAI_4
                MsgboxAsync("It runs fine", "Result")

        End Select
    Else
        Log(Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Sub Button1_Click
    'It Runs fine
    ExecuteRemoteQuery("SELECT * FROM MyTable WHERE champs1 = 1",ESSAI_1 )
End Sub

Sub Button2_Click
    Dim val1, val2, val3 as String
    Val1 = "Hello"
    val2 = "The"
    val3 = "World"

    'It Runs fine
    ExecuteRemoteQuery("INSERT INTO MyTable (champs1, champs1, champs1) VALUES ('" & Val1 & "', '" & Val2 & "','" & val3')",ESSAI_2)
End Sub

Sub Button3_Click
    Dim val1, val2, val3 as String
    Val1 = "Hello"
    val2 = "The"
    val3 = "World"

    'Synta error
    ExecuteRemoteQuery("SELECT * FROM MyTable WHERE champs1 ='" & Val1 & "' ,ESSAI_3)

    'Synta error
    ExecuteRemoteQuery("SELECT * FROM MyTable WHERE champs1 ='" & Val1 & "' ,ESSAI_3)
    
    ' Here, Erreur, l'objet a été transformé en variable string (Idem if I remplace Array as string by Array as Object)
    ExecuteRemoteQuery("SELECT * FROM MyTable where champs1 =  ? and champs2 = ?", Array As String (val1, val3), CHECK)
End Sub

Sub Button4_Click
    'IWhat is the syntax ?
    ExecuteRemoteQuery("SELECT COUNT MyTable ................,ESSAI_4)
End Sub
 

Mahares

Expert
Licensed User
Longtime User
I'm just looking for this exact code. Who knows it?
Change this line:
B4X:
ExecuteRemoteQuery("Select Count(*)  FROM countries", COUNT_ROWS)
to
B4X:
ExecuteRemoteQuery("Select Count(*) AS cnt FROM countries", COUNT_ROWS)

Then use the below code to populate the label with the 230 records. That should do it.
B4X:
Case COUNT_ROWS           
                Dim root As List = parser.NextArray
                For Each colroot As Map In root
                    Dim cnt As String = colroot.Get("cnt")
                    LblCR.Text = cnt
                Next
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
The problem is simple. I cannot display the number of records contained in a remote PfpMyAdmin database.
If I go to the Erel tutorial at this address: https://www.b4x.com/android/forum/threads/connect-android-to-mysql-database-tutorial.8339/#content he explains how to connect to a table and collect the value of the different fields in the table, but not how to get the number of records in the table.
I modified its Mysql.zip attachment to show you what I want to do.
Look at the zip attachement and run the program . Click on Button1 should display the number of records in the table, but code to add in the "Select COUNT_ROWS" of the Sub Jobdone is missing. I'm just looking for this exact code. Who knows it?
Thank you
B4X:
SELECT COUNT(*) NUMROWS FROM .....
NUMROWS will be the number of rows
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Isn't that the answer in post #21. What is the purpose of duplicating answers.
Missed that one. There was an earlier one that didn't NAME COUNT(*) which I figured would be confusing.
I had the page open for a while before responding. Perhaps you wrote your answer while I was writing mine.
Note that i was responding to a post that was PRIOR to your post.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Perhaps you wrote your answer while I was writing mine.
No big deal. My post was June 9th and yours June 28th. The problem with threads that have too many posts especially when some are redundant, they become unreadable and hard to follow. The shorter the sweeter.
 
Upvote 0
Top