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
Sqlite.jpg

Displayed on tableView, 1 unwanted decimal appears
My_result.jpg


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
Did anyone understand exactly the problem? 😄

(first post)

View attachment 88743

I undestand so:
1) db field WS is declared as Float but contains only Integers
2) when he, using DBUtils, fills the TableView, one digit is shown!

Trying to help you... I "worked too much" testing B4XTable.
Take a look at the project I'm attaching; I fear it is not perfect (mainy the B4X column types / SQLite column types association).


[The project can be a good basis for managing a generic table whose contents are unknown.]
Wonderful work
Will I ever be able to repay

Quando vieni a Genova
Focaccia pagata 😜
 
Upvote 0

Giacomo

Active Member
Licensed User
Longtime User
If this is just the problem, I don't see any reason to complicate the solution.
You know the field name [WS] and its type in the database [REAL], as well as the representation you want [INT], so why not just modify the first line of your query?

B4X:
Querry= "Select   CAST( [main].[WS_HEADER].[WS] AS INT ) AS WS, " ...
I really didn't know this 🧐
I'm afraid there are too many things I don't know .....😭
 
Upvote 0
Top