Android Question Adding result of sqlite select query to list

padvou

Active Member
Licensed User
Longtime User
Hello,
how could I add the rows returned by an sqlite select query to a list as list rows?
Thank you
 

Myr0n

Active Member
Licensed User
Longtime User
Check this tutorial please.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
example of copy a result set (from SQL lib) into a user defined type and then add to a list

B4X:
Sub Class_Globals
  
   Type Inspection(InspectionId As Int, Title As String, Customer As String, Project As String, Order As String, OrderPos As String, Description As String, Notes As String,   Name As String,   State As String,   City As String,   Zip As String,   Country As String, Latitude As String, Longitude As String, Address As String, IsGPSCoordinates As String, AppId As String, Date As Long)
End Sub

public Sub LoadAll(Filter As String)
   
    If Filter="" Then
        Filter="%"
    Else
        Filter="%" & Filter & "%"
    End If
   
    Dim List1 As List
    List1.Initialize
   
    Dim DB1 As DB
    DB1.Initialize
   
    Dim sql1 As SQL = DB1.Open
   
    Dim rs As ResultSet = sql1.ExecQuery2("SELECT * FROM Inspection WHERE NAME LIKE ? ORDER BY NAME",Array As String(Filter))
    Do While rs.NextRow
        Dim Item11 As Inspection
        Item11 = FromDB(rs)
       
        List1.Add(Item11)
       
    Loop
    rs.Close
   
    DB1.Close
   
    Inspections = List1
   
End Sub

Sub FromDB(rs As ResultSet) As Inspection
   
    'Log("FromDB")
   
    Dim Item As Inspection
    Item.Initialize
   
    Item.InspectionId = rs.GetInt("InspectionId")

    Item.Title = rs.GetString("Title")

    Item.Description = rs.GetString("Description")
    Item.Notes = rs.GetString("Notes")
   
    Item.Customer = rs.GetString("Customer")  
    Item.Project = rs.GetString("Project")
    Item.Order = rs.GetString("Order")
    Item.OrderPos = rs.GetString("OrderPos")
   
    Item.Name = rs.GetString("Name")
    Item.Address = rs.GetString("Address")
    Item.Zip = rs.GetString("Zip")
    Item.City = rs.GetString("City")
    Item.State = rs.GetString("State")
    Item.Country = rs.GetString("Country")

    Item.Latitude = rs.GetString("Latitude")
    Item.Longitude = rs.GetString("Longitude")
    Item.IsGPSCoordinates = rs.GetString("IsGPSCoordinates")

    Item.Date = rs.GetLong("Date")

    Item.AppId = rs.GetString("AppId")

    Return Item
   
End Sub

db class
B4X:
'...
Sub Class_Globals
    Private SQL1 As SQL
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   
    #IF B4A  
    If File.Exists(Path,FileName)= False Then
        File.Copy(File.DirAssets,"Template.db",Path,FileName)
        ToastMessageShow("Copied new Database from Template",True)
    End If
    #End If
   
End Sub

Public Sub Open() As SQL

    Log(Path)
    SQL1.Initialize(Path, FileName, False)
   
    Return SQL1
   
End Sub

Public Sub Close
   
    SQL1.Close
   
End Sub

Sub Path As String
   
    Dim rtp As RuntimePermissions
   
    Return rtp.GetSafeDirDefaultExternal("db")
   
End Sub

Sub FileName As String
       
    Return "Inspections.db"
   
End Sub
 
Upvote 0
Top