1 (edited by lara.0080 2024-03-10 06:56:00)

Topic: problem in date (my first project)

hi everyone

i am new to the app and i am trying some concept . i use a table called tempx that have a date filed , and when i  chose a date from date time picker and click save button the date stored in table but i have two problems
if i chose the same date it will store it again and i don't want this , i try to use sql but i have to change the date to string and i don't want that , is there a solution for this ??
second think i want to save each row in tempx date to other table with some other info, how to do this ???

i have attached the program ....

Post's attachments

Attachment icon test1.zip 335.33 kb, 25 downloads since 2024-03-10 

Re: problem in date (my first project)

lara.0080 wrote:

hi everyone

i am new to the app and i am trying some concept . i use a table called tempx that have a date filed , and when i  chose a date from date time picker and click save button the date stored in table but i have two problems
if i chose the same date it will store it again and i don't want this , i try to use sql but i have to change the date to string and i don't want that , is there a solution for this ??
second think i want to save each row in tempx date to other table with some other info, how to do this ???

i have attached the program ....

procedure Form1_Button2_OnClick (Sender: TObject; var Cancel: boolean);
begin
  if ( SQLExecute('SELECT COUNT(id) FROM tempx WHERE Date='+ Form1.DateTimePicker1.sqlDateTime) <> 0) then Cancel := True;
end;

Re: problem in date (my first project)

Hi Lara0080, Vladimir,
What exactly are you trying to do by copying dates from 'tempx' to another table? 
It always concerns me when I read something like this because it often indicates that the data schema is not optimised or correctly designed which can cause problems as you develop your application.
Also, try to avoid using field names such as 'date' (use 'mydate' or 'tempdate' for example);  the word 'date' might be used in a script to perform some operation and therefore clash (apart from being a bit confusing).
Derek.

Re: problem in date (my first project)

thank you

procedure Form1_Button2_OnClick (Sender: TObject; var Cancel: boolean);
begin
  if ( SQLExecute('SELECT COUNT(id) FROM tempx WHERE Date='+ Form1.DateTimePicker1.sqlDateTime) <> 0) then Cancel := True;
end;

COUNT(id)    what this do ???
<> 0) then Cancel := True;  --   this to check that date picker not empty , right  ???

and how to save from DateTimePicker1 to table direct as date , do you have idea ???


Thanks Again

Re: problem in date (my first project)

Hi Lara0080, Vladimir,
What exactly are you trying to do by copying dates from 'tempx' to another table?
It always concerns me when I read something like this because it often indicates that the data schema is not optimised or correctly designed which can cause problems as you develop your application.
Also, try to avoid using field names such as 'date' (use 'mydate' or 'tempdate' for example);  the word 'date' might be used in a script to perform some operation and therefore clash (apart from being a bit confusing).
Derek.

Dear derek

i am sure that my data schema is  good but my problem was with the MVD , as i try to save to table via script but it goes always as string and even with stringtodate  and datetostring  i get lot of problems , my idea is to take all date's and save them in the table by one action after choosing all  the date's , MVD elements didn't help me a lot .....

Thanks again

Re: problem in date (my first project)

COUNT(id)    what this do ???
<> 0) then Cancel := True;  --   this to check that date picker not empty , right  ???

and how to save from DateTimePicker1 to table direct as date , do you have idea ???

1.  COUNT(id) FROM tempx WHERE Date='+ Form1.DateTimePicker1.sqlDateTime) <> 0)
This checks to see if the date you are entering already exists (<> 0) as you don't want to allow duplicates.  You could write count(id) or count(*) or use any mandatory field - it's just to use something that will always be present in the record.
2.  <> 0) then Cancel := True;
This cancels the 'save' action if a duplicate date exists (ie <> 0) - if it is 0 then the record is saved.  If you want to prevent an empty date from being saved, then specify it as mandatory in your data schema.
3.  SQLite does not have a native 'date' format - dates are stored as strings and converted by the program into any date format that you specify at run time.
Derek.

Re: problem in date (my first project)

Dear Derek

Thank you very much for your excellent explain ,,, but even after this code the date could be duplicated in the table !!! and i try to remove (Cancel := True;) and use begin  else but the same problem the date can be duplicated

1.  COUNT(id) FROM tempx WHERE Date='+ Form1.DateTimePicker1.sqlDateTime) <> 0)
This checks to see if the date you are entering already exists (<> 0) as you don't want to allow duplicates.  You could write count(id) or count(*) or use any mandatory field - it's just to use something that will always be present in the record.
2.  <> 0) then Cancel := True;
This cancels the 'save' action if a duplicate date exists (ie <> 0) - if it is 0 then the record is saved.  If you want to prevent an empty date from being saved, then specify it as mandatory in your data schema.
3.  SQLite does not have a native 'date' format - dates are stored as strings and converted by the program into any date format that you specify at run time.
Derek.

Re: problem in date (my first project)

Here is the code



   begin
                       code1.Edit3.Text  := DateToStrcode1.DateTimePicker2.DateTime);
                           if ( SQLExecute('SELECT COUNT(id) FROM tempx WHERE Date1='+ code1.DateTimePicker2.sqlDateTime) <> 0) then
                           begin
              SQLExecute('INSERT INTO tempx (Date1) VALUES("'+FormatDateTime('yyyy-MM-DD', code1.DateTimePicker2.datetime)+'")');
                        newRow := absent_teacher.TableGrid1.RowCount;
                   code1.TableGrid1.InsertRow(newRow);
                 code1.TableGrid1.Cells[0, newRow] := code1.Edit3.Text ;
  end;

Re: problem in date (my first project)

Hi,
I'm not sure from your messages and attached code whether you want to update 'works' every time a new record(date) is inserted into 'tempx' or whether you want to just insert into 'tempx' one record(date) at a time and then update 'works' in batch mode.
Attached are both options.
Regards,
Derek.

Post's attachments

Attachment icon test1 options.zip 875.4 kb, 27 downloads since 2024-03-13 

Re: problem in date (my first project)

Hi Derek


Lara.0080, First we need to explain about the format.
Date and DateTime are stored in the SQLite database as "2024-03-12 12:25:09.000". If you want to compare only the date you need to extract the date from this format. In SQLite you can use the Date() function. If you do not select the date, then changing any number over time will not allow you to make a correct comparison.
In your case it will look something like this
WHERE Date (Date1)=Date ('+ code1.DateTimePicker2.sqlDateTime +' )
You need to become more familiar with SQLite.


Also note that Derek uses sqlDate in his examples, which allows you to store date + time with zeros in the database. This makes later comparisons easier. However, if you are storing real time, you will need to use the Date() function in SQLite.

Re: problem in date (my first project)

To clarify, you're encountering issues with potential duplicate dates in your SQLite database. Instead of using Date1 directly in your SQL queries, consider using the Date() function to extract just the date portion for accurate comparisons. This ensures that you're comparing dates correctly and helps avoid duplications caused by time variations. Additionally, understanding SQLite's date handling intricacies can provide insights into effective database management strategies.

smile