Database not saving record

mleffler

Member
Licensed User
Longtime User
I'm using Margaret's Db code and have setup two databases. It is very handy for what I am doing. One for readings and one for settings.
It seems the settings one does not store the record correctly.

Sub Set_SettingsDb
db.Pointer = -1
db.FileName = "settings.dat"
db.Field(0,0) = "servername" :db.Field(0,1)=50
db.Field(1,0) = "serverport" :db.Field(1,1)=10
db.Field(2,0) = "username" :db.Field(2,1)=50
db.Field(3,0) = "password" :db.Field(3,1)=20
db.Field(4,0) = "toaddress" :db.Field(4,1)=50
db.Field(5,0) = "subject" :db.Field(5,1)=50
db.Field(6,0) = "body" :db.Field(6,1)=100
db.Field(7,0) = "" :db.Field(7,1)=0
End Sub
Sub Set_ReadingsDb
db.Pointer = -1
db.FileName = "readings.dat"
db.Field(0,0) = "serialnumber" :db.Field(0,1)=20
db.Field(1,0) = "reading1" :db.Field(1,1)=10
db.Field(2,0) = "reading2" :db.Field(2,1)=10
db.Field(3,0) = "reading3" :db.Field(3,1)=10
db.Field(4,0) = "reading4" :db.Field(4,1)=10
db.Field(5,0) = "dateread" :db.Field(5,1)=30
db.Field(6,0) = "datapacket":db.Field(6,1)=30
db.Field(7,0) = "" :db.Field(7,1)=0
End Sub

I setup the two Subs above to switch between them and they seem to work Ok. The problem seems to be when I leave the program and then go back in. The Settings are first read into the program, but they dont exist when I go back in.

Am I doing something wrong or should I store the database someplace other than dir.Internal?

I have tried this on the emulator and a tablet, they both do the same thing.
I did notice that when I run this in the debugger the Settings database when first opened always has 0 records no matter how many times I start a new debug session. I assume this is normal for the debugger.

Any ideas on what could be going on would be helpful.
 

mleffler

Member
Licensed User
Longtime User
More information on database issue.

Sub ExitSettingsButton_Click
Dim ServerString As String
Dim PortString As String
Dim UserNameString As String
Dim PasswordString As String
Dim ToAddressString As String
Dim SubjectString As String
Dim EmailBodyString As String

Set_SettingsDb
ReadingsFlag = 0

'Save Settings to database record.
ServerString = ServerText.Text
PortString = PortText.Text
UserNameString = UserNameText.Text
PasswordString = PasswordText.Text
ToAddressString = ToAddressText.Text
SubjectString = SubjectText.Text
EmailBodyString = EmailBody.Text
db.Field(0,0) = ServerString
db.Field(1,0) = PortString
db.Field(2,0) = UserNameString
db.Field(3,0) = PasswordString
db.Field(4,0) = ToAddressString
db.Field(5,0) = SubjectString
db.Field(6,0) = EmailBodyString

If db.RecordCount = 0 Then
db.AddRecord()
Else
db.UpdateRecord()
End If

What I want to happen is that if the record exists it should update the record otherwise it will create a new record.
What I am seeing is the at the start of this right after the Set_SettingsDb the db.RecordCount is 0. At the end of this sub the db.RecordCount = 6 or 7 or some other number. It should be one since I am only adding one record.

The text is just whatever the user enters in text fields on a screen.

I have used the db library before without any issues so I dont know what is going on. This is however the first time I used it with more than one database in a program.

Thanks for any help you can provide.
F.Y.I. I am using version 1.6 of the code.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Hello,

I see your problem. The pointer from the start will equal -1. This is it's value before the database is created. The pointer only gets updated once records have been saved. So the code that you had will never add a new record. Try this code and it should work:

B4X:
If db.RecordCount < 1 Then
      db.AddRecord()
Else
      db.UpdateRecord()
End If
 
Upvote 0

mleffler

Member
Licensed User
Longtime User
Found the solution

I finally figured out what is going on.

It seems that if I change the database structure. For example if you go from 7 fields to 5 fields the structure wont change in any already used database.

For example I set up "readings.dat" with 7 fields then run a debug session. Next I remove 2 fields and run another session. The database will still be 7 fields in size and of course will not work. It seems to persist even if I go out of Basic4Android then back in.

However if I rename the database file to "readings2.dat" the database will have 5 fields and work correctly. It seems that any existing database once used can not be restructured. Or I haven't figured out how anyway.

I did not see a function like that anyway.

Renaming the "settings.dat" to "settings2.dat" seems to have fixed the problem I was having.
 
Upvote 0
Top