Actually, the best way to do this is by just appending a line to the ZQuery object like this: (syntax not checked)
Code: Select all
ZQuery.append;
ZQuery.FieldByName('first').Asinteger := 1;
...
ZQuery.post;
That way the ZQuery component builds the sql needed to insert the data.
The other way is what you did, but requerying afterwards:
Code: Select all
ZQuery1.SQL.Text :=
'INSERT INTO `customers` (`First`,`Last`,`eMail`,`Street`,`ZIP`,`PCity`) VALUES'+
'(''1'', ''2'', ''3'', ''4'', ''5'', ''6'');';
ZQuery1.ExecSQL;
ZQuery1.SQL.Text := 'SELECT * FROM customers;';
ZQuery1.Active := True;
This is not very efficient, however as the refresh is done to fill the query object with only on more row, the one you've just inserted.
What happens in my example:
- You query the database and fill the ZQuery object with data
- Data is shown in the grid,
linked to the ZQuery data buffers
- A row of data is added to the buffer by the Append statement
- The grid is automatically updated from the buffer
- The post sends an insert statement to the database
What happens in your example:
- You query the database and fill the ZQuery object with data
- Data is shown in the grid,
linked to the ZQuery data buffers
- You clear the data buffer of the ZQuery object by ExecSQL. (which sets Active:=false)
- The record is inserted in the database
My second solution adds
- You query the database again and fill the ZQuery object with data
- Data is shown in the grid,
linked to the ZQuery data buffers
Actually, you should study the database handling that's standard in Delphi using TQuery objects. The 'Using databases' (or something alike) chapter is quite good and clear on the basic concepts of using datasets.
(Just to make you even more curious : apart from Append, there's also Edit and Delete which do all the work for you)
Mark