Android Question Json response & SQL statements

tufanv

Expert
Licensed User
Longtime User
Hello,

I have a little problem. I am getting data from websocket and according to that websocket data, I use sql to update my database. My problem is, lets say I have normally 16 fields in a websocket data I receive, I update mysql database with this :

B4X:
sql1.ExecNonQuery("UPDATE borsa set last='" & last & "',low='" & low & "',high='" & h & "',ask='" & a & "',bid='" & b & "',dailychange='" & change & "',dailychangepercent='" & changepercent & "',timeupdated='" & t & "',weekchange='" & wc & "',weekpercent='" & wp & "',monthchange='" & mc & "',monthpercent='" & mp & "',yearchange='" & yc & "',yearpercent='" & yp & "',previousclose='" & pc & "',totalvolume='" & tV & "' WHERE kod='" & istekid & "' ")

There is no problem with the first data received from the websocket because websockets sends all fields at first request but after that, It only send the field that changed, for example it sends dailychange and last value, so when this happens the code above gives and error. If i put it between try catch, app does not crash but it also does not update any field. How can I adopt my code to dynamcally update the fields only received ? Currently this is what I do when I receive data from websocket:

B4X:
Dim parser As JSONParser
            parser.Initialize(someText)
            Dim root As Map = parser.NextObject
            Dim a As Double = root.Get("a")
            Dim b As Double = root.Get("b")
            Dim tV As Double = root.Get("tV")
            Dim change As Double = root.Get("c")
            Dim changepercent As Double = root.Get("C")
            Dim mp As Double = root.Get("mp")
            Dim h As Double = root.Get("h")
            Dim yc As Double = root.Get("yc")
            Dim last As Double = root.Get("l")
            Dim low As Double = root.Get("L")
            Dim wc As Double = root.Get("wc")
            Dim pc As Double = root.Get("pc")
            Dim t As Int = root.Get("t")
            Dim mc As Double = root.Get("mc")
            Dim yp As Double = root.Get("yp")
            Dim wp As Double = root.Get("wp")
            Dim istekid As String = root.Get("_i")
            sql1.ExecNonQuery("UPDATE borsa set last='" & last & "',low='" & low & "',high='" & h & "',ask='" & a & "',bid='" & b & "',dailychange='" & change & "',dailychangepercent='" & changepercent & "',timeupdated='" & t & "',weekchange='" & wc & "',weekpercent='" & wp & "',monthchange='" & mc & "',monthpercent='" & mp & "',yearchange='" & yc & "',yearpercent='" & yp & "',previousclose='" & pc & "',totalvolume='" & tV & "' WHERE kod='" & istekid & "' ")

When only some of these fields arrives, problem begins because sql cant find the value to update.
 
Top