How to display TIMEDIFF (now, then) in a Grid database?

Sorry, I'm very new to DbGrids.

Should I use the query field editor and somehow add a new field that captures TIMEDIFF and then just add this as a column in my DbGrid?

Or can / should I skip the field editor and somehow declare TIMEDIFFF as a column?

For this table, I want a DbGrid with 4 columns: start time, end time, duration, description (run_id is the primary key and will not be displayed).

I am focused on how to get the data in the "duration" column ...

mysql> describe test_runs; +------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+-------------+------+-----+---------+----------------+ | run_id | int(11) | NO | PRI | NULL | auto_increment | | start_time_stamp | timestamp | YES | | NULL | | | end_time_stamp | timestamp | YES | | NULL | | | description | varchar(64) | YES | | NULL | | +------------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.37 sec) 

[Update] Query for data source

 SELECT start_time_stamp, end_time_stamp, TIMEDIFF(end_time_stamp, start_time_stamp) as duration, description FROM test_runs ORDER BY start_time_stamp DESC 

and when I run it manually in MySql, I get

 mysql> select TIMEDIFF(end_time_stamp, start_time_stamp) as duration FROM +----------+ | duration | +----------+ | NULL | | 00:04:43 | | 00:00:13 | | 00:00:06 | | 00:00:04 | +----------+ 5 rows in set (0.00 sec) 

but the corresponding column in the database grid remains empty. Can anyone help? Thanks.


[Update] I am using AnyDac if this helps. The query creates all the fields, including the time difference, in MySql, as well as in the Delphi environment, when I use the AnYDac query editor and execute it.

The only problem is that I do not see it in the DB grid at runtime. I double click on the DB tree at design time and the columns are correct. The FielName FielName set to duration , which is reconfigured by the query shown above. It does not exist in the database, but is calculated on request; can this somehow be a problem?


[Aaaaaaaargh !!!] Someone tried to "improve" my code and programmatically program the query text at runtime (like SELECT * FROM test_runs) , thereby overwriting my design time query !! Since the databse table has no duration , none were shown in the database table.

There were words, the voices were dirty, and now I have to refuse, spending your time. Unfortunately.

+4
source share
3 answers

I would create a calculated field in your query and add this field to your DbGrid.

therefore, as you say, with an open field editor for the query, right-click and select "New Field" (or press Ctrl-N). Give your new field a name (for example, Duration), keep the default component name, or rename if you want. Set the type as needed (perhaps in this case DateTime) and set the type of the field to calculate.

Then, in the OnCalcFields event of your request, set the value of this field to the value you need. eg:

 procedure TForm1.Query1CalcFields(DataSet: TDataSet); begin Dataset.FieldByName('description').AsDateTime := DataSet.FieldByName('end_time_stamp').AsDateTime - DataSet.FieldByName('start_time_stamp').AsDateTime; end; 

Or you can also specify the value Duration as an additional field in the selected query. Unfortunately, I do not have ready access to MySQL here, but it could be something like:

 select run_id, start_time_stamp, end_time_stamp, description, (end_time_stamp - start_time_stamp) as duration from test_runs; 
+7
source

Should I use the query field editor and somehow add a new field that captures TIMEDIFF and then just add this as a column to my DbGrid?

Do not delete all entries from this field editor first

Or can I / should skip the field editor and somehow declare TIMEDIFFF as a column?

Do not delete all entries from the column editor first

After that, you should see all the columns from the open dataset inside the grid, because you exclude all restrictions from the column editor and the field editor.

As you can guess what happens to your grid / data, you did not set the FieldName property for the duration column, and therefore the grid did not know what to represent.

+3
source

To use DBgrid, you need a data source, each data set must be assigned to the corresponding components and make sure that you have established a connection. if you didn’t receive anything in the grid, you need to check your data set, if only the data of the difference column by date is missing, but the header displays the name of the field in the request, maybe you can get null data ..... even if you didn’t get the name header, you need to check your request .......

+2
source

All Articles