B4J Question [Solved]intermittent sql query

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All

I do not know if I have done something wrong as far as formatting goes , Sometimes this works and other times not.

I thought I had ironed out the bugs , but they came back.

The problem is the 2nd lookup , there are NO results. The first one works perfectly and the second one works every now and then. There is not even a pattern. I even deleted the source tables and recreated them with no luck.

Any you have any ideas, please let me know

Thank you

B4X:
Sub stk_List
If DB.sql1.IsInitialized = False Then
        DB.sql1.InitializeSQLite(File.DirApp, "data/diamonds.db", True)
    End If
    DateTime.DateFormat="yyyy/MM/dd/"
    Log(DateTime.DeviceDefaultDateFormat)
Dim ra As ResultSet       
    ra = DB.Sql1.ExecQuery2("SELECT sku, carat, clarity,colour, shape, barc, sale_percent,costdiscount,price_carat ,name, Avail, usd_rapp FROM Stock WHERE Avail = ?", Array As String(0))
    Do While ra.NextRow
   
        Log("_______________________________")
        Log("Barcode "&ra.GetString("barc"))
        Log("SKU "&ra.GetString("sku"))
        Log("Description "&ra.GetString("name"))
        Log("USD Rapp "&ra.GetString("usd_rapp"))
        Log("Carat "&ra.GetDouble("carat"))
        Log("Price/Carat "&ra.GetString("price_carat"))
        Log("Avail "&ra.GetString("Avail"))
        Log("Colour "&ra.GetString("colour"))
        Log("Clarity "&ra.GetString("clarity"))
        Log("Shape "&ra.GetString("shape"))
'   
        Dim rb As ResultSet
   
        rb=DB.Sql1.ExecQuery2("SELECT shape, low_size, high_size, color, clarity ,caratprice, date FROM price WHERE shape = ? and color = ? and clarity = ? and low_size <= ? and high_size >= ? ",Array As String((ra.GetString("shape")),(ra.GetString("colour")),(ra.GetString("clarity")),(ra.GetDouble("carat")),(ra.GetDouble("carat"))))

        Do While rb.NextRow

            Log("=====================================================")
            Log("Low Size "&rb.GetDouble("low_size"))
            Log("High Size "&rb.GetDouble("high_size"))
            Log("RR Clarity "&rb.GetString("clarity"))
            Log("RR Colour "&rb.GetString("color"))
            Log("RR Shape "&rb.GetString("shape"))
            Log("RR Price/Carat "&rb.GetString("caratprice"))
            Log("-----------------------------------------------------")
            DateTime.DateFormat="yyyy/MM/dd"
            Log(DateTime.DeviceDefaultDateFormat)
           
            Dim xr As ResultSet
            xr=DB.Sql1.ExecQuery2("SELECT Date, Rate FROM xrate WHERE Date LIKE ? ",Array As String(DateTime.Date(DateTime.Now)))
           
            Log(DateTime.Date(DateTime.Now))
            Do While xr.NextRow
                Log("Rate "&xr.GetDouble("Rate"))
           
                Dim zarprice As Double
                Dim minusd As Double
                Dim minzar As Double
                Dim cost As Double
                Dim usdprice As Double
           
            Dim newusd As Double
            newusd=Round(rb.GetDouble("caratprice") * ra.GetDouble("carat"))
            Dim zarapp As Double
            zarapp=Round(newusd * (xr.GetDouble("Rate")))
           
                cost = Round(newusd * 0.55)'((100 - ra.GetDouble("costdiscount"))/100))
                zarprice = Round((cost * ra.GetDouble("sale_percent"))*xr.GetDouble("Rate"))
                usdprice = Round(cost * ra.GetDouble("sale_percent"))
                minusd = Round(cost * 1.02)
                minzar = Round(minusd *  xr.GetDouble("Rate"))
               
            Dim def As Map
            def.Initialize
            def.Put("price_carat", rb.GetDouble("caratprice"))
            def.Put("usd_rapp", newusd)
            def.Put("RappDate", rb.GetString("date"))
            def.Put("zar_rapp", zarapp)
            def.Put("xrate", xr.GetDouble("Rate"))
            def.Put("xRateDate",DateTime.Date(DateTime.Now))
            def.Put("usd_cost",cost)
            def.Put("zar_price", zarprice)
            def.Put("MinSellUSD", minusd)
            def.Put("MinSellZAR", minzar)
            def.Put("usd_price", usdprice)
            def.Put("price", 0)
           
            Dim flt As Map
            flt.Initialize
           
            flt.Put("Barc", ra.GetString("barc"))
                       
            DBUTILS.UpdateRecord2(DB.sql1,"Stock",def,flt)
       

            Loop
           
            xr.Close
       
           
        Loop
        rb.Close
   
    Loop
    ra.Close
   
   
    Main.Label2.Text= "Done"

End Sub
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
isnt this nullifying each other?

B4X:
low_size <= ? and high_size >= ?

'Maybe corret: 
low_size >= ? and high_size <= ?
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
isnt this nullifying each other?

B4X:
low_size <= ? and high_size >= ?

'Maybe corret:
low_size >= ? and high_size <= ?

I took out all the filters and went back to the plain query command and it does go through all the records correcly. Maybe I should not use the ? and enter all the filters in the main body of the query.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I took out all the filters and went back to the plain query command and it does go through all the records correcly. Maybe I should not use the ? and enter all the filters in the main body of the query.

you can try but i do not think so, everything seems correct, may be there are no records with those filters.

Are you using any SQL manager? for SQLite i recommend:
https://sqlitestudio.pl/index.rvt

if you want, you can PM the DB and i can run some tests.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
WHERE shape = ? and color = ? and clarity = ? and low_size <= ? and high_size >= ?
That is a lot of "and-ing" in the expression. Could it just be that there are no results that actually match the given criteria? And the
low_size <= ? and high_size >= ?
is correct (the value is greater (or equal) than the low_size, but lesser (or equal) than the high_size).

You need to get a "ra" resultset for which the "rb" resultset shows no data and really see if there is data in "rb" that matches the criteria found in "ra". Until then, we don't really know if anything is going wrong.

Beaten to it by @Enrique Gonzalez R (I'm slow...)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The databse has definately all the values to match or it would not have worked before.
In this post (https://www.b4x.com/android/forum/threads/sql-search-query-solved.79798/) you said you had issues and that the carat values where text. If they still are text, you will get strange results and may have to use "casting" (see https://www.sqlite.org/lang_expr.html#castexpr) to convert everything to REAL or NUMERIC before doing the compare.

Instead

rb=DB.Sql1.ExecQuery2("SELECT shape, low_size, high_size, color, clarity ,caratprice, date FROM price WHERE shape = ? and color = ? and clarity = ? and low_size <= ? and high_size >= ? ",Array As String((ra.GetString("shape")),(ra.GetString("colour")),(ra.GetString("clarity")),(ra.GetDouble("carat")),(ra.GetDouble("carat"))))

B4X:
rb=DB.Sql1.ExecQuery2("SELECT shape, low_size, high_size, color, clarity ,caratprice, date FROM price WHERE shape = ? and color = ? and clarity = ? and CAST(low_size AS REAL) <= ? and CAST(high_size AS REAL) >= ? ",Array As String((ra.GetString("shape")),(ra.GetString("colour")),(ra.GetString("clarity")),(ra.GetDouble("carat")),(ra.GetDouble("carat"))))
Please note that this may cause some slight rounding issues (see http://grokbase.com/t/cloudera/impala-user/14awrenrjr/cast-string-to-double-float)
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I think the problem was 2 fold.

Firstly when I regenerated the price file, it did not specify any field types , so I assume they were text, and when it did not work I went back and took all the LIKE commands and replaced them with = . I noticed now that the one file it was looking at was "Round" and the other file "round" I think the LIKE command sorted that out in the past. That is why I initially took out all the filters except the first one and it did not work. The first one had the Round problem. If it had only been the format of the SQLite fields , I would have fault found it.

So this is what I have now and it is working

B4X:
rb=DB.Sql1.ExecQuery2("SELECT shape, clarity, color,low_size, high_size, caratprice, date FROM price WHERE shape LIKE ? and clarity LIKE ? and color LIKE ? and low_size <= ? and high_size >= ?", Array As String(ra.GetString("shape"),ra.GetString("clarity"),ra.GetString("colour"),ra.GetDouble("carat"),ra.GetDouble("carat")))

I need to reasearch if I should convert to REAL as was suggested.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
LIKE command sorted that out in the past
A couple of notes on SQLite's LIKE:
 
Upvote 0
Top