Android Question MariaDB does not wait for query results

RenewMe

Member
HI, i,am using Mariadb for read/write tables and it works perfect, but my problem is that the application goes further without waiting for the query results.
Is there someone who can explain me better please.
Thank you
 

DonManfred

Expert
Licensed User
Longtime User
How should we help? You are providing ZERO information on how you are doing it now.

Upload a small project showing the problem.

Use the async methods and use waitfor to wait for the result.
 
Last edited:
Upvote 0

RenewMe

Member
Hi DonManfred , this is code:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    
    
    Activity.LoadLayout("2")
    Activity.Title="NBS Morena Srl"
    
    attivo=0
    png1.Initialize(LoadBitmap(File.DirAssets, "LogoBigMorena.png"))
    Logo.Background=png1
    nrdev=p.GetSettings("android_id")
    macDisp.Text=nrdev
    modello = p.Manufacturer & " " & p.Model & " " & p.GetSimOperator
    Label1.Text = modello
    StartActivity(MariaDB)
    If attivo=0 Then
        ToastMessageShow("Device not allowed",True)
        Activity.Finish
    End If

    '

In the below MariaDB activity I take the value of variable 'attivo' (0 or 1 values) from the central Maria database, that tells me if device is allowed.
MariaDB activity

MariaDB activity:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim MYSQLIP = "xxx.xxx.xxx.xxx" As String
    Dim MYSQLDBNAME = "xxxxxxxx"  As String
    Dim MYSQLPORT = "0000"  As String
    Dim MYSQLUSER = "xxx"  As String
    Dim MySQLPASS = "xxxxxxx"  As String
    Dim MySQLConnection As MariaDBConnector
    Dim vuoto,vuoto3,vuoto2,vuoto4 As Boolean
    Dim vettura As Int
    Dim annop As Int,ngiro As Int,nrdev3 As String,idvettura As Int
    Dim ora As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.Color=Colors.Transparent
    If MySQLConnection.IsInitalized =False Then
        MySQLConnection.Initialize(MYSQLIP,MYSQLDBNAME,MYSQLUSER, MySQLPASS,MYSQLPORT)
    End If
    '
    DateTime.DateFormat="yyyy"
    annop=DateTime.Date(DateTime.Now)
    DateTime.DateFormat="yyMMdd"
    ngiro=DateTime.Date(DateTime.Now)
    DateTime.DateFormat="yyyy-MM-dd"
    '
    Log("data1 cerca " & DateTime.Date(DateTime.Now))
    '
    vuoto3=True
    nrdev3 = Main.nrdev
    MySQLConnection.ExecQuery("query3","select * from vetture where mcaddr='" & nrdev3 & "'" )

End Sub

Sub Activity_Resume
    Log(" MariaDB resume")
    'StartActivity(MariaDB2)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Log(" MariaDB in pausa")
    'StartActivity(Main)
    'Activity.Finish
End Sub

Sub query3_complete(finished1 As Boolean)
    Dim ora As String
    Log("query3 complete " & finished1)
    If vuoto3=True Then
        MySQLConnection.ExecNonQuery("query2","insert into vetture (mcaddr,nomevettura) select '" & nrdev3 & "','" & "Camion " & nrdev3 & "'")
    End If
    Activity.Finish
    'StartActivity(MariaDB2)
End Sub

Sub query3_update(product1 As Map)
    Log("query3 update " & product1.Get("idvettura"))
    Main.attivo=product1.Get("attivo")
    idvettura=product1.Get("idvettura")
    Main.Nrvett=idvettura
    vuoto3=False
End Sub

Sub query3_error(trace1 As String)
    Log("Errore query3: " & trace1)
End Sub

After in the 'if' check it and shutdown app if not allowed.
In my case the flow passes over and the control does not work.
I'm looking at your valuable suggestion (waitfor).

Many thanks.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Check this:

 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello @RenewMe
I've created an example for you that I hope others will also find useful and simple enough to figure out.

Btw you don't need to create query1, query2, query3 etc etc etc, just query will do. Here is a quick example (not tested in B4A but should work) that should get you started. Put the .jar file in your additional libraries folder and adjust the code to meet your needs.

I've attached MariaDB ConnectorJ V1.1.8 as that was the version number that popped into my head. I know that MariaDB ConnectorJ V1.1.8 works perfect with JdbcSQL, actually you can use up to and including MariaDB ConnectorJ V2.0.3 with absolutely no issues whatsoever in B4A. DO NOT USE MariaDB ConnectorJ 2.1.x and above in B4A as they cause B4A build errors. If you need to use 'useSSL' in your connection string, you can download MariaDB ConnectorJ V2.0.3 as B4A works perfect with ConnectorJ V2.0.3 and 'useSSL'.


Enjoy...
 

Attachments

  • mariadb-java-client-1.1.8.jar
    237.1 KB · Views: 316
  • MariaDBExample.zip
    9.3 KB · Views: 350
Last edited:
Upvote 0

RenewMe

Member
Hello @RenewMe
I've created an example for you that I hope others will also find useful and simple enough to figure out.

Btw you don't need to create query1, query2, query3 etc etc etc, just query will do. Here is a quick example (not tested in B4A but should work) that should get you started. Put the .jar file in your additional libraries folder and adjust the code to meet your needs.

I've attached MariaDB ConnectorJ V1.1.8 as that was the version number that popped into my head. I know that MariaDB ConnectorJ V1.1.8 works perfect with JdbcSQL, actually you can use up to and including MariaDB ConnectorJ V2.0.3 with absolutely no issues whatsoever in B4A. DO NOT USE MariaDB ConnectorJ 2.1.x and above in B4A as they cause B4A build errors. If you need to use 'useSSL' in your connection string, you can download MariaDB ConnectorJ V2.0.3 as B4A works perfect with ConnectorJ V2.0.3 and 'useSSL'.


Enjoy...
Thank you Peter, sorry for my delay but was very busy, asap i'll test your suggestions and let you know.
 
Upvote 0
Top