Board index » delphi » Adding a row with TQuery

Adding a row with TQuery

Scenario: Database table with many rows; don't wish to retrieve all rows

into TDBGrid; so you open a form with the grid, and the TQuery is
inactive (i.e. no rows displayed in grid); you might provide a custom
query
option to query the db to retrieve rows; cached updates and updateSQL
components are used.
Problem: What if the user opens the form and wants to add a row instead
of querying; the TQuery is inactive.
Question: Is there a way to add a row when the TQuery is inactive
( i.e. how do you add a row without first activating the dataset and
retrieving 0..* row(s))? Any alternative solutions that are geared
towards a generic framework viewpoint where I can't make direct
references to specific instances of TwwQuery or it's SQL statement?

Michael Sumption
Shasta County I.S., CA
r...@snowcrest.net

 

Re:Adding a row with TQuery


You can use a Tquery object and add a row to the table by doing this:

query.Sql.Add('Insert into yourtable(Col1, Col2,Col3) values('Blah', 'Blah',
'Blah');
query.ExecSql;

This will insert a record into the table without you having to make a query
object active.

--
Michael Glatz
mgl...@caiso.com

Quote
r...@snowcrest.net wrote in message <36E6E94F.CD18B...@snowcrest.net>...
>Scenario: Database table with many rows; don't wish to retrieve all rows

>into TDBGrid; so you open a form with the grid, and the TQuery is
>inactive (i.e. no rows displayed in grid); you might provide a custom
>query
>option to query the db to retrieve rows; cached updates and updateSQL
>components are used.
>Problem: What if the user opens the form and wants to add a row instead
>of querying; the TQuery is inactive.
>Question: Is there a way to add a row when the TQuery is inactive
>( i.e. how do you add a row without first activating the dataset and
>retrieving 0..* row(s))? Any alternative solutions that are geared
>towards a generic framework viewpoint where I can't make direct
>references to specific instances of TwwQuery or it's SQL statement?

>Michael Sumption
>Shasta County I.S., CA
>r...@snowcrest.net

Re:Adding a row with TQuery


Michael,

If you don't want any rows back from the server but still want to use the
Insert method, and have TUpdateSQL do the work for you then try this:

Make sure the TQuery.SQL property is something like

SELECT * FROM Mytable WHERE 1=2

Then set the TQuery.Active := True and do your inserting.

HTH

Jim

Quote
r...@snowcrest.net wrote:
> Scenario: Database table with many rows; don't wish to retrieve all rows

> into TDBGrid; so you open a form with the grid, and the TQuery is
> inactive (i.e. no rows displayed in grid); you might provide a custom
> query
> option to query the db to retrieve rows; cached updates and updateSQL
> components are used.
> Problem: What if the user opens the form and wants to add a row instead
> of querying; the TQuery is inactive.
> Question: Is there a way to add a row when the TQuery is inactive
> ( i.e. how do you add a row without first activating the dataset and
> retrieving 0..* row(s))? Any alternative solutions that are geared
> towards a generic framework viewpoint where I can't make direct
> references to specific instances of TwwQuery or it's SQL statement?

> Michael Sumption
> Shasta County I.S., CA
> r...@snowcrest.net

Re:Adding a row with TQuery


One way to do this which adds some flexibility to you code is to set your
TQuery SQL properity like this...

SET ROWCOUNT = :rowcount
SELECT * FROM MYTABLE

Then you can set the :rowcount parameter to 0 or whatever you would like.

Patrick

Other Threads