B4J Question [BANano] [SOLVED] How can I pass a BANanoSQL as a variable to a sub?

alwaysbusy

Expert
Licensed User
Longtime User
BANanoSQL is not a real variable but kind of an atom object (there is no 'new'). You do not have to pass it to a sub. Just like the BANano Object, you just define it in the Globals of the class and 'assign' your database to it with the OpenWait() command.

SQL.OpenWait("SQL", "MyDB") just 'assigns' the MyDB database to the B4J SQL variable.

So, you can have in your Main:
B4X:
Sub Process_Globals
   Public SQL As BANanoSQL    
   Public myDB As MyDBFuncs
End Sub

Sub BANano_Ready()
    ' Initialize your local browser database
    SQL.OpenWait("SQL", "MyDB")
    SQL.ExecuteWait("CREATE TABLE IF NOT EXISTS tTable (tblid INT, tblcode STRING, tbldesc STRING)", Null)
    
    myDB.Initialize
    
    BANano.Await(myDB.InsertWait(1, "A", "Alain"))
    BANano.Await(myDB.InsertWait(2, "J", "Jos"))
    
    Dim Results As List = SQL.ExecuteWait("SELECT tblcode, tbldesc FROM tTable", Null)
    Log(Results)
    ...
End Sub

And a class MyDBFuncs:
B4X:
Sub Class_Globals
    Private BANano As BANano 'ignore
    Private SQL As BANanoSQL
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub

public Sub InsertWait(id As Long, code As String, desc As String)
    Dim Vars As List
    Vars.Initialize
    Vars.Add(id)
    Vars.Add(code)
    Vars.Add(desc)
     
   '  just 're-assign' MyDB to the local SQL variable
    SQL.OpenWait("SQL", "MyDB")
    SQL.ExecuteWait("INSERT INTO tTable (tblid, tblcode, tbldesc) VALUES (?, ?, ?)", Vars)
End Sub

Alwaysbusy
 
Upvote 0
Top