Recent

Author Topic: [Solved] SQL update statement and CheckBox  (Read 1236 times)

folkeu08

  • Full Member
  • ***
  • Posts: 114
[Solved] SQL update statement and CheckBox
« on: January 19, 2025, 10:31:34 pm »
Hi,
In an SQL update statement, I update the value of a checkbox with this piece of code:
 
Code: Pascal  [Select][+][-]
  1. +', Present=' + quotedstr(BoolToStr(Present_CheckBox.Checked)) + '
when the checkbox is checked, the value of Present is '-1'. I assume this is a consequence of the BoolToStr conversion.
Is there a method to get 1?
I tried to convert to absolute value (abs) but the types are incompatible.
Should I really do a test on the status of the chekbox and hard-code 0 or 1 with a variable in the update command ?
Thanks
François
« Last Edit: January 22, 2025, 10:11:47 am by folkeu08 »

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: SQL update statement and CheckBox
« Reply #1 on: January 19, 2025, 11:18:09 pm »
Code: Pascal  [Select][+][-]
  1. +', Present=' + quotedstr(IntToStr(Ord(Present_CheckBox.Checked))) + '

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: SQL update statement and CheckBox
« Reply #2 on: January 19, 2025, 11:19:47 pm »
Or like this if you want '1' for false and '0' for true:

Code: Pascal  [Select][+][-]
  1. +', Present=' + quotedstr(IntToStr(1 - Ord(Present_CheckBox.Checked))) + '

egsuh

  • Hero Member
  • *****
  • Posts: 1533
Re: SQL update statement and CheckBox
« Reply #3 on: January 20, 2025, 05:17:49 am »
You should not use "quotedStr".  The SQL  you should write :

   update tbl1 set Present=true where .... , not Present = 'true'.

AFAIK integer 0 is regarded as false while any other value is interpreted as true. So, if your table has a Boolean field, then you don't have to mind the returned integer value from Lazarus App.

Or,

     + ' Present=' + BoolToStr(Present_CheckBox.Checked, true)

which will return string 'True' or 'False' depending on the checkbox's status.

Zvoni

  • Hero Member
  • *****
  • Posts: 2821
Re: SQL update statement and CheckBox
« Reply #4 on: January 20, 2025, 07:58:38 am »
You should not use "quotedStr".  The SQL  you should write :

   update tbl1 set Present=true where .... , not Present = 'true'.

AFAIK integer 0 is regarded as false while any other value is interpreted as true. So, if your table has a Boolean field, then you don't have to mind the returned integer value from Lazarus App.

Or,

     + ' Present=' + BoolToStr(Present_CheckBox.Checked, true)

which will return string 'True' or 'False' depending on the checkbox's status.
This would be true, if sqlite did have a Boolean-Type, which it doesn't.

@TS: If you have a "Boolean"-Column in your Database-Table, set its DataType to integer, Default=0
Then from your code you can use Parameters, with "0" being False, everything else being true, by assigning the CheckBox-Value directly
Any "displaying" of "True/False" on your Form is then a simple thing
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: SQL update statement and CheckBox
« Reply #5 on: January 20, 2025, 10:22:46 am »
You should not use "quotedStr".  The SQL  you should write :

Maybe he wants to have 0 and 1 for false and true. If you mentioned not to use 'QuotedStr', better advice would be to use parameters, something like this:
Code: Pascal  [Select][+][-]
  1.     +', Present = :present';
  2.   ParamByName('present').AsInteger := Ord(Present_CheckBox.Checked);

Zvoni

  • Hero Member
  • *****
  • Posts: 2821
Re: SQL update statement and CheckBox
« Reply #6 on: January 20, 2025, 11:03:07 am »
or if he showed us the FULL SQL-Statement, we would better equipped to advise better....

There is another Concatenation preceding the snippet (notice the "+" at the beginning), as well as following the snippet
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

silvercoder70

  • Full Member
  • ***
  • Posts: 138
    • Tim Coates
Re: SQL update statement and CheckBox
« Reply #7 on: January 20, 2025, 10:52:49 pm »
Random thought, Use this version ...

function BoolToStr(B: boolean; const TrueS, FalseS: string): string;

and call as

BoolToStr(boolean-value, '1', '0');
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

folkeu08

  • Full Member
  • ***
  • Posts: 114
[Solved] Re: SQL update statement and CheckBox
« Reply #8 on: January 22, 2025, 10:10:59 am »
Hello everyone,
Thank you for your help.
I tried the newest solutions to the oldest and stopped at the first one that worked.
The proposal of 'dseligo' with this code:
Code: Pascal  [Select][+][-]
  1. +', Present=' + quotedstr(IntToStr(Ord(Present_CheckBox.Checked))) +

I started with the following code but the values ​​were reversed (I had not read the remark at the beginning of the post). I had made an adaptation:
Code: Pascal  [Select][+][-]
  1. +', Present=' + quotedstr(IntToStr(0 - Ord(Present_CheckBox.Checked))) +
Thanks
François

« Last Edit: January 22, 2025, 10:13:17 am by folkeu08 »

 

TinyPortal © 2005-2018