Generate SQL on runtime

Discussion of open issues, suggestions and bugs regarding IBDAC (InterBase Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
rdollinger
Posts: 7
Joined: Mon 26 Oct 2009 12:55

Generate SQL on runtime

Post by rdollinger » Wed 04 Nov 2009 10:56

Hi,

I would handle different db-tables with one TIBCQuery. Is there a method to generate the sql's (update, insert, delete) on TIBCQuery on runtime?

thanks for help in advanced

bye
Robert

benek
Posts: 20
Joined: Sun 06 Jul 2008 14:22

Post by benek » Wed 04 Nov 2009 11:39

in Delphi
//select
TIBCQuery.SQL.Clear;
TIBCQuery.SQL.ADD('SELECT FIELD_NAME FROM TABLE');
TIBCQuery.OPEN;
IF NOT TIBCQuery.ISEMPTY THEN
BEGIN
FIELD := TIBCQuery.FIELDBYNAME('FIELD_NAME').VALUE;
END;
TIBCQuery.CLOSE;

//delete,update
TIBCQuery.SQL.Clear;
TIBCQuery.SQL.ADD('DELETE FROM TABLE where ...');
TIBCQuery.ExecSQL;

rdollinger
Posts: 7
Joined: Mon 26 Oct 2009 12:55

Post by rdollinger » Wed 04 Nov 2009 11:51

Ok that's clear. But that is not what I have answerd for.

I have many master-tables on my db, and I would not type for any of this master-tables the SQLUpdate, SQLInsert and SQLDelete statments. Designtime there is the SQL Generator on TIBCQuery to do this.

Because I don't wont use a separate TIBCQuery component for each table, I have to generate this SQL-Statements dynamically on runtime.

Is there a solution for this?

bye
Robert

benek
Posts: 20
Joined: Sun 06 Jul 2008 14:22

Post by benek » Wed 04 Nov 2009 12:22

Eeeee.. I don't anderstand
"Because I don't wont use a separate TIBCQuery component for each table.."

You don't need to use a separate one, only change the sql statement in runtime
TIBCQuery.SQL.Clear;
TIBCQuery.SQL.ADD('... new statement

and open or execute

rdollinger
Posts: 7
Joined: Mon 26 Oct 2009 12:55

Post by rdollinger » Wed 04 Nov 2009 13:15

But in this way I have to change every time the code of my programm. :(
And I would avoid this with automating this procedure:

Pass the table name on my procedure
I create the "select * from table_name" in the way you have explained
At this point I don't know how many fields this table has
The missing part: create via a method (like SQL Generator) the update, insert and delete statements.

I hope I have explained it more clearly now :P

bye
Robert

benek
Posts: 20
Joined: Sun 06 Jul 2008 14:22

Post by benek » Wed 04 Nov 2009 13:51

rdollinger wrote:... I hope I have explained it more clearly now :P
Yes, You want to make life more complicated :lol:

Remember You can use params in sql statement (for instance for tables)
and iterate through fields in result set you are getting back

Good luck !

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Thu 05 Nov 2009 09:02

The TIBCQuery components generates the update SQLs automatically if you have empty values in the SQLInsert, SQLUpdate, and SQLDelete properties.

So you just need to clear these properties if you want to use one TIBCQuery component for different queries.

Post Reply