Topic: How to Wrtie Time to Database Using SQLExecute

Hello,
I would like the command to write the current time to a database field using SQLExecute please.

SQLExecute('INSERT INTO TestData (DataDate) VALUES (' + Time() +')');

At one stage I had it working so that it would put a time in the field but where it is 4:15PM here, it would have the time in the database as 3:15AM. Can anyone help please.
Thanks in advance, David

2 (edited by mathmathou 2018-01-17 06:34:11)

Re: How to Wrtie Time to Database Using SQLExecute

Hello radoft


Assuming your Time() is a TDateTime how do you get this variable ?


Did you try something like :

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
    timeNow : Extended;
begin
    timeNow := Time; //Time is system time and should respect your systems settings regarding the format
    SQLExecute('INSERT INTO TestData(DataDate) VALUES("'+TimeToStr(timeNow)+'")');
end;


If you get the Time variable directly you can even simplify like this:

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
    SQLExecute('INSERT INTO TestData(DataDate) VALUES("'+TimeToStr(Time)+'")');
end;

Let me known if this helps.


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: How to Wrtie Time to Database Using SQLExecute

Hello Mathias,
Thank you for the help. I can try this out in the morning and report back to you.
Regards,
David

Re: How to Wrtie Time to Database Using SQLExecute

Hello Mathias,
In reality, my project involves various types of data such as Data, Time, Text, and numbers. There is hardware that forms the entire string and I receive the data through the serial port. The string is comma delimited and I get the various fields out of the string. The date and time is not part of the string received in the PC.
I wish to write the received data into a table using SQLExecute. The data and time part of it are just the local date and time on the computer and I would like to write this local date and time into the database for each record. The data is received in close to real real time so that is fine.
I attached an example so I could practice writing various data types into the file but it all went wrong and aren't sure where so I removed most of my SQLExecute and would appreciate it if you could complete that line for me.
In the test project attached, there are boxes to write a text and enter a integer and real number and these should all be written into the data file just one record at a time as the Add button is pressed.
I hope that makes sense.
Also, the SQLExecute lines become quite long so could you tell me the best way to spread a SQL command over multiple lines please.
No pressure at all, I am only progressing as I can and as others are able to help.
Thanks and regards,
David

Post's attachments

Attachment icon SQLInsert.zip 336.41 kb, 369 downloads since 2018-01-17 

Re: How to Wrtie Time to Database Using SQLExecute

Hello radsoft,


A quick and "dirty" modification of your project just to give you something. I'll try to improve that tonight.


Query is written on multiple lines to show you how to spread it over multiple lines (beware of spaces between some instructions : if you forget them, the query will fail).


Also, I changed the Date format of your date field to text : the date format was not showing up in the dbgrid even with data in database. I will investigate tonight too.


Last thing : you talk about a comma separated list you receive through COM port right ?
Do you know how to handle those strings and turn them into "smaller bits" to save them in database ? Have you ever used StringLists ?


Anyway, back to work now smile


Cheers


Math

Post's attachments

Attachment icon SQLInsert.zip 335.15 kb, 426 downloads since 2018-01-18 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: How to Wrtie Time to Database Using SQLExecute

Hello Math.
Thank you. I have so far dabbled with it but now I am being asked to do so much more so it's essential I start learning this.
I break up the data string with SplitString which seems to work ok. I will look at the file in the morning that you sent.
Thank you for the help.
Regards, David

Re: How to Wrtie Time to Database Using SQLExecute

Hello Math,
thank you for the demo code. It inserts the various fields so can I learn from the format of your command.
I don't understand why you have made the date field into a TEXT field. Where a field is to be a date, is there some sort of rule to apply to determine when the field type should be a DATE or TEXT? What about if SQL SELECT commands are used on the data file and the field containing a date is a TEXT field?
Is there some condition where a field containing a time should be a TEXT field?
Thanks again for your help, really appreciate it.
Regards,
David

8 (edited by derek 2018-01-19 00:10:52)

Re: How to Wrtie Time to Database Using SQLExecute

Hi David,
It's not really a question of 'date' OR 'text'.  'Date' is just a data type, not a storage class (valid storage classes are 'text', 'real', 'integer', 'blob' and 'null'). 
So before your 'date' data type is saved, it first needs to be converted into one of these storage classes - in this case, to 'text', which is why Mathias has to do the conversion.  And the same holds for the 'time' data type.
I hope this helps to clarify it a bit.
Derek.

Re: How to Wrtie Time to Database Using SQLExecute

Hello Derek,
Hope you are well. Yes, thank you for helping make it clear. I have managed to incorporate the help from Mathias into my project and I am now storing the data in the data file automatically as it arrives in the serial port.
I break up the incoming stream using the SplitString command and I write that into TEXT fields in the data file. The telemetry data is numeric . I have a button that displays the data file fields in a spreadsheet. The spreadsheet shows the data as text, not numeric entries in the spreadsheet cells and that means I can't use a spreadsheet to do graphs. There might be some way to convert all spreadsheet cells to numbers in one go, I don't know. That possibly means I need to convert the sql fields to numeric.
Anyway, I am pleased with progress and I can show the customer next week.
Regards,
David

Re: How to Wrtie Time to Database Using SQLExecute

Hello both of you smile


If you want to convert a type of data into another before exporting it to Excel, you might want to look at the CAST instruction.


For example, if you want to retrieve a text field composed of numbers to a number, you can try :

     SQLExecute(‘SELECT CAST(my_field AS INTEGER) FROM my_table’);

THis should give you an integer recognized as such by Excel, provided the data is an integer. If it is a REAL you can use the CAST AS REAL.


Have a good weekend both of you


Cheers


Math

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor