Android Question JdbcResultSet to ListOfMaps

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

Is there an easy way to get the JdbcResultSet to a List of Maps ? Without having to mention the column names and data type ?

I am trying to read data from a Remote MySQL database and then write this data to the internal SQLite DB on the Android Phone via DBUtils.

In one of my older projects, I was using the MySQL lib provided by DonManfred and using that lib I was able to insert the data to the SQLite DB using the following few lines of code as shown below
B4X:
Sub MyDB_QueryResult(Data As List, Meta As Map)
 
    Dim ListOfMaps As List
    ListOfMaps.Initialize
 
    For i=0 To Data.Size-1
        Dim cur As Map = Data.Get(i)
        ListOfMaps.Add(cur)
    Next
    Starter.Sql1.ExecNonQuery("DELETE FROM MyTable")
    'Write the data to SQLite DB via DBUtils
    DBUtils.InsertMaps(Starter.Sql1, "mas_items", ListOfMaps)
 
End Sub
Now I am wondering how can I transfer the JdbcResultSet data to SQLite DB utilising the DBUtils.

The following is the code that I have written so far. Not sure whether I am re-inventing a wheel and if there is already an easy solution to this issue.
B4X:
Dim SenderFilter As Object =  mysql.ExecQueryAsync("SQLK", cSql, Null)
Wait For (SenderFilter) SQLK_QueryComplete (Success As Boolean, rs As JdbcResultSet)
If Success Then
    Dim ListOfMaps As List
    ListOfMaps.Initialize
    Do While rs.NextRow
         
        Dim cur As Map
        cur.Initialize
        For i=0 To rs.ColumnCount-1
            'In the next line comes the problem. How do I know whether the value in the column is Int, double, Long OR String ?
            'Accordingly I have to make the call rs.GetInt OR rs.GetLong OR rs.GetDouble OR rs.GetString
            cur.Put( rs.GetColumnName(i), rs.GetInt( rs.GetColumnName(i) ) )
         
        Next
        ListOfMaps.Add(cur)
             
    Loop
    rs.Close

End If
I am sure that there must be a better way of handling this, hence asking for support.
Any help will be appreciated
 
Last edited:

calloatti

Member
Licensed User
Everything you need is in the attached class. Example usage:


B4X:
'Creates sqlite table if needed:
Main.db.MysqlToSqlite("mysqltable","sqlitetable", False)

dim result as boolean

result = Main.db.MysqlToSqliteData("mysqltable", "sqlitetable", "", "", False)

Column names and types are handled by the class
 

Attachments

  • db.bas
    10.6 KB · Views: 161
Upvote 0
Top