sqlite to dictionary -> key not found exception

I try to save query from SQLLITE to dictionary with following code, but i get error key not found exception, dictionary is also empty.

  Using Xojo.Core
  Dim d As  New Dictionary
  
  Dim dbFile As FolderItem
  Dim db As New SQLiteDatabase
  dbFile = GetFolderItem("db.sqlite")
  db.DatabaseFile = dbFile
  //If Not DBConnect Then Return
  If db.Connect Then
    Dim orders As RecordSet
    orders = db.SQLSelect ("SELECT * FROM customers ORDER BY id DESC LIMIT 1")
    If db.Error Then
      
    Else
      If orders <> Nil Then
        While Not orders.EOF
          d.Value("id")   = orders.Field("id").StringValue
          d.Value("customer_business_unit")   = orders.Field("customer_business_unit").StringValue
          d.Value("date_time")   = orders.Field("date_time").StringValue
           orders.MoveNext
        Wend
      End if
    End if
  End if
  
  msgbox (d.Value("customer_business_unit"))

You’re probably getting the key error on that MsgBox line…
Unless your orders recordset is only going to have one entry, it’s going to keep overwriting the same three keys… Maybe make an array of dictionaries?

DIM d() As Dictionary . . . while not orders.of DIM e As NEW Dictionary e.Value("id") = e.Value("customer_business_unit") = e.Value("date_time") = d.Append e wend

Hi @John Walker

Have you tried to debug that chunck of code?

Maybe it reach the messagebox line without populating the dictionary… you can also try to move the ’msgbox (d.Value("customer_business_unit"))' line after the ‘wend’. That way you’ll be sure that the Dictionary is populated.

Other option you can try is:

msgbox d.lookup("customer_business_unit","")

That way it will show the value in case there is the passed ‘key’, or an empty string if the dictionary has not the they.

Javier

Hi

The code works ok, It was my typo mistake in code.

Thank you.