Android Question Putting the values of a sql record into b4xtable

Hi
I am hoping someone can help with what I thought was a simple problem,and it may very well be but I can't seem to solve it
I have a sql db record with many fields, some of which are the same name but with a number on the end, e.g Field1, Field2 etc
I can read all the fields ok and the values show correctly when I log them but I cannot add them to the table
My coding is simply

B4X:
    Dim row(2) As Object
    For x = 1 To 4
        Dim Col As String="Field" & x
        Dim Score1 As Int =B4XPages.MainPage.rst.Getstring(Col)
''        Dim Score2 As Int =B4XPages.MainPage.rst.Getstring(Col)
        row(0) =Score1
''        row(1) =Score2
        Log(Score1)
        Data.Add(row)
    Next
After a couple of iterations I get the log message


I forgot to mention that a result is shown but it's just the last number printed on every line. How do i get each field on a seperate line?
 
Solution
It's a simple issue but I understand how I missed it
I need to re-dim the object each time within the loop not outside as I had done. Thanks for the help Mahares
Now Works:
    'inner loop for fields
    For x = 1 To 4
       Dim row(2) As Object
        Dim Col As String="Field" & x
        Dim Score1 As Int =B4XPages.MainPage.rst.Getstring(Col)
''        Dim Score2 As Int =B4XPages.MainPage.rst.Getstring(Col)
        row(0) =Score1
''        row(1) =Score2
        Log(Score1)
        Data.Add(row)
    Next

BTW how do I mark the issue as resolved?
Sorry I tried to remove the pic but I was timed out for editing but the log is like this
After a couple of iterations I get the log message
1 (> Field Value)
3 (> Field Value)
4 (> Field Value)
Warning: same object added to list multiple times
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Can you show your full code on how you iterate through the SQLite table records and how you iterate through the columns and how you finally add them to the B4Xtable. Looking at you code, It seems to me like you you need to take this line: Data.Add(row) and put it after the the Next, so you can include all the table fields instead of only one, but there is not enough info for me to be sure.
 
Upvote 0

zed

Active Member
Licensed User
Hi Jack
You forgot B4XTable1.SetData(data) after the loop.

Example:

B4X:
Do While RS.NextRow

    Dim row(5) As Object
    row(0) = RS.GetInt("id")
    row(1) = RS.GetString("field_1")
    row(2) = RS.GetString("field_2")
    row(3) = RS.GetString("field_3")
    row(4) = RS.GetString("field_4")
    data.Add(row)

Loop
RS.Close
B4XTable1.SetData(data)
 
Upvote 0
Thanks for the responce,
"take this line: Data.Add(row) and put it after the the Next, so you can include all the table fields instead of only one, but there is not enough info for me to be sure.
I actually tried that. As for all the records I am just testing with the one record because multiples didn't work either. I should be able to pull all the fields of one record and show then as a single item in a row but it doesn't work. As I said the fields are retrieved ok and show in the logging window but I cannot get them to be added to the table

the code is the same for all fields but with a higher iteration number.
This code works for adding multiple records and gives me the fields as columns and records as rows
B4X:
Do While B4XPages.MainPage.rst.NextRow
        row(0) =B4XPages.MainPage.rst.Getstring("P1")
        row(1) =B4XPages.MainPage.rst.Getstring("P2")
        row(2) =B4XPages.MainPage.rst2.GetInt("P3")
        row(3) =B4XPages.MainPage.rst.Getstring("P4")
        row(4) =B4XPages.MainPage.rst.Getstring("P5")
        row(5) =B4XPages.MainPage.rst.Getstring("P6")
        B4XPages.MainPage.rst.NextRow
        Data.Add(row)
LOOP
However what I want to do is add the fields as rows so if this was to give me 10 rows from 10 records with 6 fields then I would want 6 rows per record giving me 60 rows, i.e
Row 1 would be Record one field P1
Row 2 Record one field P2
Row 3 Record one field P3
Row 4 Record one field P4
Row 5 Record one field P5
Row 6 Record one field P6

Row 7 Record TWO field P1
Row 8 Record TWO field P2
...
 
Last edited:
Upvote 0
It's a simple issue but I understand how I missed it
I need to re-dim the object each time within the loop not outside as I had done. Thanks for the help Mahares
Now Works:
    'inner loop for fields
    For x = 1 To 4
       Dim row(2) As Object
        Dim Col As String="Field" & x
        Dim Score1 As Int =B4XPages.MainPage.rst.Getstring(Col)
''        Dim Score2 As Int =B4XPages.MainPage.rst.Getstring(Col)
        row(0) =Score1
''        row(1) =Score2
        Log(Score1)
        Data.Add(row)
    Next

BTW how do I mark the issue as resolved?
 
Upvote 0
Solution

Mahares

Expert
Licensed User
Longtime User
Glad you got it solved. But, can you show a snapshot of the first page of the way the data looks in the B4Xtable. I have never seen data displayed the way you figured it out on a B4XTable.
To mark the thread as solved, you need to edit the title by adding [SOLVED] at its beginning.
 
Last edited:
Upvote 0

I am unable to edit the title, is there a button somewhere?
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I am unable to edit the title, is there a button somewhere?
To edit your title, click on your thread. On the top right you should see 3 dot to the right of the word 'Watch'. Click on the 3 dots and select 'Edit thread'
Thanks for posting a snapshot of the table. If it made sense to you, then that is all it counts, because I have not learned anything from it. I love to see a final snapshot when you fully include all meaningful columns.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…