Android Tutorial [B4X] B4XTable - Load data from SQL database

B4XTable.SetData expects a List where each entry in the list is an array of objects with the data of a single row.

The data can come from any source you like.

SS-2019-02-10_15.49.29.png


Example of loading the data from a SQL database:
B4X:
B4XTable1.AddColumn("Customer Id", B4XTable1.COLUMN_TYPE_NUMBERS)
   B4XTable1.AddColumn("Name", B4XTable1.COLUMN_TYPE_TEXT)
   B4XTable1.AddColumn("Company", B4XTable1.COLUMN_TYPE_TEXT)
   B4XTable1.AddColumn("Address", B4XTable1.COLUMN_TYPE_TEXT)

   Dim Data As List
   Data.Initialize
   Dim rs As ResultSet = sql.ExecQuery("SELECT CustomerId, FirstName, LastName, Company, Address FROM customers")
   Do While rs.NextRow
       Dim row(4) As Object
       row(0) = rs.GetDouble("CustomerId")
       row(1) = rs.GetString("FirstName") & " " & rs.GetString("LastName")
       row(2) = rs.GetString("Company")
       'Some of the values are Null. We need to convert them to empty strings:
       If row(2) = Null Then row(2) = ""
       row(3) = rs.GetString("Address")
       Data.Add(row)
   Loop
   rs.Close
   B4XTable1.SetData(Data)
Step #1: add the columns.
Step #2: fill the Data list with the data coming from the SQL db.
Step #3: call B4XTable.SetData.

B4A project is attached.

Database source: http://www.sqlitetutorial.net/sqlite-sample-database/
 

Attachments

  • B4XTableFromSQL.zip
    336.6 KB · Views: 4,473

Omar Moreno

Member
Licensed User
Longtime User
Greetings.

The library is very good.
You could show the code to load a column with SQLITE BLOB type values.

Thank you.
 

harinder

Active Member
Licensed User
Longtime User
Do we have to define column headers for b4xtable separately? Can b4xtable pick up entire db with headers as used to be the case in Flexitable----"table1.LoadSQLiteDB(SQL1, Query, True)". in which case headers were not separately defined for Table1? thnx
 
Top