B4J Question Problem with Float field in db

Giacomo

Active Member
Licensed User
Longtime User
Ciao
I have to read a sqlite db table and insert it into a tableview.
B4X:
Sub Table_01_SelectedRowChanged(Index As Int, Row() As Object)
    Row= Table_01.SelectedRowValues
    Log(Row(3))
    Dim Querry As String
    
    Querry= "Select    [main].[WS_HEADER].[WS], "
    Querry=Querry & "[main].[WS_HEADER].[CUSTOMER_CODE], "
    Querry=Querry & "[main].[CUSTOMERS].[CUSTOMER_NAME_1], "
    Querry=Querry & "[main].[CUSTOMERS].[CUSTOMER_NAME_2], "
    Querry=Querry & "[main].[CUSTOMERS].[CITY],"
    
    Querry=Querry & "[main].[WS_HEADER].[AGENT_CODE], "
    Querry=Querry & "[main].[WS_HEADER].[AGENT_DESCR] "
    Querry=Querry & "FROM   [main].[WS_HEADER] "
    Querry=Querry & "INNER JOIN [main].[CUSTOMERS] ON [main].[WS_HEADER].[CUSTOMER_CODE] = [main].[CUSTOMERS].[CUSTOMER_CODE] "
    Querry=Querry & "WHERE  [main].[WS_HEADER].[WS_YEAR] = 2019 "
    Querry=Querry & "And [main].[WS_HEADER].[AGENT_CODE] = '"&Row(3)&"';"
    
    DBUtils.ExecuteTableView(SQL1,Querry,Null,0,Table_02)
            
    Log(Table_02.Items.Size)
End Sub
the problem is in reading the [WS] field( is Type FLOAT ), the value entered in the DB has no decimal

Displayed on tableView, 1 unwanted decimal appears


Unfortunately I can't change the structure of the db.
Can you help me ?
 

udg

Expert
Licensed User
Longtime User
Something like this could work?
B4X:
For Each row() As Object In tvMyTable.Items
'data field to display as integer
        Dim p1 As Pane = row(6)   '<-- adapt to your table
        Dim lbl As Label
        lbl = p1.GetNode(0)      
        Dim i As Int = lbl.text
        lbl.text = NumberFormat2((i,1,0,0,False)
The above assumes a label as the content of each field in that tables' data row.
See here for a TableView Tutorial

A example on how to modify the original DBUtils code in order to have labels instead of strings:
B4X:
'A TableView having an integer ID in column 0
Public Sub ExecuteTableView(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        values(0) = cur.GetInt2(0)  ' store ID as integer!
        For col = 1 To cur.ColumnCount - 1
            Dim lbl As Label
            lbl.Initialize("")
            lbl.Text = cur.GetString2(col)   'every other field stores ad a label
            'align center/right based on original data type
            If IsNumber(lbl.Text) Then
                 values(col) = cmCommons.WrapLabel(lbl, "CENTER_RIGHT")
            Else
                 values(col) = cmCommons.WrapLabel(lbl, "CENTER_LEFT")
            End If
        Next
        TableView1.Items.Add(values)    'save whole record
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit  'take care of eventual size limit for table rows
    Loop
    cur.Close
End Sub
 
Last edited:
Upvote 0

Giacomo

Active Member
Licensed User
Longtime User
Wonderful work
Will I ever be able to repay

Quando vieni a Genova
Focaccia pagata ?
 
Upvote 0

Giacomo

Active Member
Licensed User
Longtime User
I really didn't know this ?
I'm afraid there are too many things I don't know .....?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…