Toggle button .checked = true

barx

Well-Known Member
Licensed User
Longtime User
Hi

Can anybody explain this strange behaviour.

I am saving a record for a database I use

B4X:
Record.Put("sound", toggleSound.Checked)

When i paused on this line it shows that toggleSound.Checked = true

Yet when I read the field from database I get an error because the field = 1 Which cannot parse to true or false.

I know I can parse it myself but why does it show as true/false in ide on pause yet save in database as 1/0.

Database field is a BOOL btw

thanks
 

barx

Well-Known Member
Licensed User
Longtime User
Ah, I see. Never noticed that. Sure I have manually set true / false before and worked ok. Though I can't have if it doesn't support it. Lol

Guess its time for my int2bool lib to make an appearance.

Thanks for reply

Sent from my HTC Desire Z
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
OK, done a little more investigating and found this...

if I wrap the true/false in ' ' it will save as true false. If I don't it saves as 0 / 1.

Does this mean that even though it type is set to Bool (in SQLManager) it is saving as text?
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Does this mean that even though it type is set to Bool (in SQLManager) it is saving as text?
Yes.

See Sqlite documentation here:
Datatypes In SQLite Version 3


In order to maximize compatibility between SQLite and other database engines, SQLite supports the concept of "type affinity" on columns. The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data. It is just that some columns, given the choice, will prefer to use one storage class over another. The preferred storage class for a column is called its "affinity".


Rolf
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
OK, I had this working and for some reason now it's not and I cannot get it right again.

I'm trying to save the state of a toggle button in a sqlite db and later read / update it also.

what would be the best way of doing this?
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
I personally save true/false in an Sqlite db as numerical value:
False = 0
True = 1 (or any nonzero value. Some languages also assign -1 to True).

B4X:
If value = 0 Then
  toggleSound.Checked = false
else
  toggleSound.Checked = true
End if

Rolf
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Ok going to try that, see if things settle down.
 
Upvote 0
Top