1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4A Class B4X Settings Class - Ready for B4i map from json "bug"

Discussion in 'Additional libraries, classes and official updates' started by hatzisn, Jul 31, 2019.

  1. hatzisn

    hatzisn Active Member Licensed User

    Hi everyone,

    the following is a settings class which makes it absolutely easy to save as many settings as you want in one text field in a SQLite database table. You initialize it with the field name, table name, folder of db and db file name or just with the field name, table name and an SQL and then everything is a piece of cake. Call its methods to Load settings, Save Settings, Set setting field, Get setting field, Set "Saved settings" message, Set "Settings Loaded" message. The class is ready for the B4i map from json "bug" which does not allow you to change values in a map derived from json in B4i.

    Edit - The settings table must have one row

    Sub Class_Globals
    Private sField As String
    Private sTable As String
    Private mSets As Map
    Private connSettings As SQL
    Private saveSet As String = "The settings were saved."
    Private loadSet As String = "The settings were loaded."
    #IF B4i
            Dim hd As HUD
    #End If
    End Sub

    'Initializes the object. You can add parameters to this method if needed.
    Public Sub Initialize(FieldInDBToLoadAndSaveTheSettings As String, TableInDBToLoadAndSaveTheSettings As String, FolderOfDB As String, DBFileName As String)
        sField = FieldInDBToLoadAndSaveTheSettings
        sTable = TableInDBToLoadAndSaveTheSettings
        connSettings.Initialize(FolderOfDB, DBFileName, 
    End Sub

    Public Sub Initialize2(FieldInDBToLoadAndSaveTheSettings As String, TableInDBToLoadAndSaveTheSettings As String, conn As SQL)
        sField = FieldInDBToLoadAndSaveTheSettings
        sTable = TableInDBToLoadAndSaveTheSettings
        connSettings = conn
    End Sub

    Public Sub LoadSettings
    Dim cur As ResultSet
    Dim sSettings As String
            cur = connSettings.ExecQuery(
    "SELECT " & EscapeSQLEntity(sField) & " FROM " & EscapeSQLEntity(sTable))
    Do While cur.NextRow
                sSettings = cur.GetString(sField)
    Dim jp As JSONParser
    Dim m As Map
                m = jp.NextObject
    'To avoid the bug of iOS - Maps derived from JSON cannot change
                For Each k As String In m.Keys
                    mSets.Put(k, m.Get(k))
    'take only the first row
    #IF B4A
    ToastMessageShow(loadSet, True)
    #Else If B4i
    #End If
    #IF B4A
    #Else If B4i
    #End If
    End Try

    End Sub

    Public Sub Set(SettingName As String, SettingValue As Object)
        mSets.Put(SettingName, SettingValue)
    End Sub

    Public Sub Get(SettingName As StringAs Object
    Return mSets.Get(SettingName)
    End Sub

    Public Sub SaveSettings
    Dim sSettings As String
    Dim jg As JSONGenerator
        sSettings = jg.ToString
    "UPDATE " & EscapeSQLEntity(sTable) & " SET " & EscapeSQLEntity(sField) & "=?"Array As String(sSettings))
    #IF B4A
    ToastMessageShow(saveSet, True)
    #Else If B4i
    #End If
    End Sub

    Private Sub EscapeSQLEntity(sEntityName) As String
    Dim sb As StringBuilder
    If sEntityName.StartsWith("[") = False Then
    End If
    If sEntityName.EndsWith("]") = False Then
    End If
    Return sb.ToString
    End Sub

    Public Sub setSettingsSavedMessage(sMes As String)
        saveSet = sMes
    End Sub

    Public Sub setSettingsLoadedMessage(sMes As String)
        loadSet = sMes
    End Sub
    Last edited: Jul 31, 2019
  2. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Ready to use.

    Attached Files:

    inakigarm likes this.
  3. makis_best

    makis_best Active Member Licensed User


    Thank you.
    hatzisn likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice