Problem updating database record

mleffler

Member
Licensed User
Longtime User
I am using Maragrets library which I like but from some reason I cant seem to update records, I can add them with no problem.

B4X:
Sub SaveReadingsData(SerNum As String)
Dim DateString As String
   
   db.Field(0,0) = WirelessReceiverModule.DeviceID   
   db.Field(1,0) = WirelessReceiverModule.Reading1
   db.Field(2,0) = WirelessReceiverModule.Reading2
   db.Field(3,0) = WirelessReceiverModule.Reading3
   db.Field(4,0) = WirelessReceiverModule.Reading4
   DateString = CreateTimestamp
   db.Field(5,0) = DateString
   
   If db.FindRecord(SerNum) = False Then
   'Update the record If it exists.
      db.AddRecord()
   Else
      db.UpdateRecord()
      'Adds a record. Records are sorted by field order
   End If
What I'm doing is first seaching for the record by a serial number. If it exists I want to update the data in the existing record. If it does not exist I want to add a new record.

I have confirmed that adding a record works fine but updating does not seem to work.

Any ideas would be helpful.
 

margret

Well-Known Member
Licensed User
Longtime User
Hello,

The FindRecord function does not return any value. So, the code as you have it will not work with the current code. However, you can leave you code just as it is and replace the FindRecord sub, in the db.bas module with this new FindRecord code:

B4X:
Sub FindRecord(SearchStr As String) As Boolean
   If Exist(FilePath&FileName) Then
      Dim MyList As List
      SearchStr = Trim(SearchStr.ToLowerCase)
       MyList = File.ReadList(FilePath, FileName)
      RecordCount = MyList.Size
      Dim StrToSch As String
      RPF = -1
      For l = 0 To MyList.Size -1
         StrToSch=MyList.Get(l)
         StrToSch=StrToSch.ToLowerCase
         RPF = At(StrToSch, SearchStr)
         If RPF > -1 Then
            RPF = l
            Pointer = RPF
            GetRecord
            Return True
         End If
      Next
      ToastMessageShow("No Match Was Found", False)
   Else
      ToastMessageShow("File Not Found Or No Records", False)
   End If
   Return False
End Sub
 
Upvote 0

mleffler

Member
Licensed User
Longtime User
Thanks figured it out.

I was getting it to call the db.Update but it just was not updating. Thanks for the code anyway.

I just got it to work. Seems that if I am updating I should not set db.Field(0,0). If I dont set a value in that field it works.
Must throw off a pointer somewhere if you try to update with the first field.

thanks,
 
Upvote 0
Top