Hi all,
I was looking into 'de-spaghettifying' my code that uses @DonManfred 's MsMariaDb (https://www.b4x.com/android/forum/threads/chargeable-msmariadb-another-connector-to-mysql.50732/ - highly recommended !) and this sample code seems to do the job. Perhaps it's useful for someone else too.
Cheers,
walt61
I was looking into 'de-spaghettifying' my code that uses @DonManfred 's MsMariaDb (https://www.b4x.com/android/forum/threads/chargeable-msmariadb-another-connector-to-mysql.50732/ - highly recommended !) and this sample code seems to do the job. Perhaps it's useful for someone else too.
Cheers,
walt61
B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: False
#ApplicationLabel: DeSpaghettifyDb
#VersionCode: 1
#VersionName: 1
' I keep this one on Portrait and then in Activity_Create enable
' changing the orientation, because changing it during app load
' caused an exception
#SupportedOrientations: Portrait
#CanInstallToExternalStorage: False
#DebuggerForceStandardAssets: True
#End Region
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim SQL As MSMaria ' Works with MySQL too; https://www.b4x.com/android/forum/threads/chargeable-msmariadb-another-connector-to-mysql.50732/
Dim dbhost As String = "..." ' <<< your value here
Dim dbport As String = "..." ' <<< your value here
Dim dbuser As String = "..." ' <<< your value here
Dim dbpw As String = "..." ' <<< your value here
Dim dbname As String = "..." ' <<< your value here
Dim dbtablename As String = "..." ' <<< your value here
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.
Private Button1 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Button1.Initialize("Button1")
Activity.AddView(Button1, 50dip, 50dip, 300dip, 100dip)
Button1.Text = "Connect and read db"
Button1.TextSize = 16
Button1.TextColor = Colors.White
#If RELEASE
Msgbox("You may want to switch to debug mode or you won't see what's being logged", Application.LabelName)
#End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Try
SQL.CloseDatabase
Catch
Log("SQL.CloseDatabase: " & LastException)
End Try
End Sub
Sub Activity_KeyPress(KeyCode As Int) As Boolean 'return true if you want to consume the event
Return False
End Sub
Sub Button1_Click
Log("Entered sub: " & "Button1_Click")
Try
SQL.Initialize("SQLdb", dbhost & ":" & dbport, dbuser, dbpw, dbname)
Catch
MsgboxAsync("Database init failed:" & CRLF & LastException, Application.LabelName)
Return
End Try
Wait For SQLdb_Status(Connected As Boolean, ReConnecting As Boolean, RetriesLeft As Int)
If Connected = False Then
MsgboxAsync("Database connection failed:" & CRLF & LastException, Application.LabelName)
Return
End If
ToastMessageShow("Connected to database, reading data...", False)
SQL.QueryASync("SELECT * FROM " & dbtablename, "LogAll")
Wait For SQLdb_QueryResult(data As List, meta As Map)
'Log("SQLdb_QueryResult meta:" & CRLF & meta)
'Log("MySQL_QueryResult(Columns " & meta.get("ColumnCount") & ", Records " & meta.Get("RecordCount") & "," & meta.Get("ms") & "ms," & meta.Get("TaskID"))
If meta = Null Then
MsgboxAsync("Database query error", Application.LabelName)
Return
End If
If data.Size < 1 Then
MsgboxAsync("No data were found", Application.LabelName)
Return
End If
Dim taskID As String = meta.Get("TaskID")
Select Case taskID.ToLowerCase
Case "logall"
LogAllData(data, meta)
End Select
End Sub
Sub LogAllData(data As List, meta As Map)
Log("Entered sub: " & "LogAllData")
Dim i As Int
Dim j As Int
For i = 0 To data.Size - 1
Log("*********** ROW " & i & " ***********")
Dim cur As Map = data.Get(i)
For j = 0 To cur.Size - 1
Log("Key: " & cur.GetKeyAt(j) & " | Value: " & cur.GetValueAt(j))
Next
Next
Msgbox("The data have been logged", Application.LabelName)
End Sub