Smarter way to run queries?

Forum related to Firebird

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
littlebigman
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: 15.05.2009, 14:33

Smarter way to run queries?

Post by littlebigman »

Hello

I'm reading "The Firebird Book" and am trying to build a very basic Delphi application to connect to a Firebird server.

I was wondering: Is there a smarter, easier way to run queries like this?

Code: Select all

  With ZQuery1 do begin
    Connection := ZConnection1;

    Sql.Clear;
    //How to check if a table already exists?
    Sql.Text := 'CREATE TABLE mytable (id  SMALLINT NOT NULL, name VARCHAR(20), primary key (id))';
    ExecSql;

    Sql.Text := 'CREATE GENERATOR gen_t1_id';
    ExecSql;

    Sql.Text := 'SET GENERATOR gen_t1_id TO 0';
    ExecSql;

    Sql.Text := 'set term !!';
    ExecSql;

    Sql.Text := 'CREATE TRIGGER T1_BI FOR mytable ACTIVE BEFORE INSERT POSITION 0 AS BEGIN if (NEW.id is NULL) then NEW.id = GEN_ID(GEN_T1_ID, 1); END!!';
    ExecSql;

    Sql.Text := 'set term !!';
    ExecSql;

    Sql.Clear;
    Sql.Add('INSERT INTO mytable (name) VALUES (:Val1)');
    Params.ParamByName('Val1').AsString := 'John Doe';
    ExecSql;

    Sql.Clear;
    Sql.Add('SELECT *  FROM mytable');
    //BAD for SELECT
    //ExecSql;
    Open;

  end;
Thank you.
trupka
Expert Boarder
Expert Boarder
Posts: 140
Joined: 26.08.2007, 22:10

Post by trupka »

You can check if table exists by using TZSQLMetadata component.
You can load entire sql script from file (SQL.LoadFromFile()) and/or use TZSQLProcessor component.
IMO, it's better to deploy already configured database then create it through application on the fly - especially with firebird (SET TERM PROBLEMS, some DDL and DML SQL statements won't work together inside transaction etc.)
Post Reply