[ZeosLib + Delphi 2007] "Hello, world!" to get sta

Forum related to Firebird

Moderators: gto, cipto_kh, EgonHugeist

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

[ZeosLib + Delphi 2007] "Hello, world!" to get sta

Post by littlebigman »

Hello

I think I managed to successfully compile and install ZeosLib 6.6.4 in Delphi 2007.

I'm having a bit of difficulty, however, getting started even with "examples\simple\ZSimpleMain.pas" because it uses a lot of extra stuff (TZSQLMonitor, TZSequence, properties, etc.), and am stuck at where to go now to connect to a FireBird server on the same host, and perform the following basic tasks:
1. Create a database (I read that it's not possible to do this directly from a client application: What is the right way to start from scratch?)
2. Create a table
3. Create a couple of rows
4. Read them back with a query
5. Attach a datasource to this dataset
6. Attach a DBGrid to the datasource
7. Let the user make changes through the DBGrid
8. Use an UpdateSQL to save the changes back to the database

Any help much appreciated :)

Thank you.
littlebigman
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: 15.05.2009, 14:33

Post by littlebigman »

At this point, I was able to create a database file, create a table within, but fail INSERTing a record and read it back:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
    MyDB,MyUSER,MyPASSWORD : String;
begin
  MyDB := 'C:\test.firebird';
  MyUSER := 'SYSDBA';
  MyPASSWORD := 'masterkey';

  With ZConnection1 do begin
    Hostname := 'localhost';
    Protocol := 'firebird-2.0';
    User := MyUSER;
    Password := MyPASSWORD;

    Database := MyDB;

    if not Connected then begin
      if not FileExists(MyDB) then begin
        Properties.Add(format('createNewDatabase=CREATE DATABASE %s USER %s PASSWORD %s PAGE_SIZE 4096 DEFAULT CHARACTER SET ISO8859_1',[QuotedStr(MyDB), QuotedStr(MyUSER), QuotedStr(MyPASSWORD)]));
      end;
      Connect;
    end;
  end;

  //Fails here
  With ZQuery1 do begin
    Connection := ZConnection1;
    Sql.Clear;
    Sql.Add('CREATE TABLE IF NOT EXISTS mytable (id  SMALLINT NOT NULL, name VARCHAR(20), primary key (id));');

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

  With ZQuery1 do begin
    Sql.Clear;
    Sql.Add('SELECT *  FROM mytable');
    ExecSql;
  end;

  ZConnection1.Disconnect;

end;
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Don't disconnect!!

And use Open instead of ExecSQL when opening a dataset (-> for select statements). ExecSQL just fires the sql to the server, but doesn't retrieve results (execpt for RowsAffected)

Did you get any error messages or does the code just complete?

Mark
Image
littlebigman
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: 15.05.2009, 14:33

Post by littlebigman »

Thanks for the help. I have a couple of questions:

1. Why is it wrong to disconnect in the same event, even after I'm done with SQL?

2. I get an error when INSERTing a record: Instead of seeing "John Doe", I get a question mark:

http://pastebin.ca/1434841

3. The next steps are to allow a user to edit the dataset within a DBGrid object, and save those changes back ot the database. Would you or someone happen to have an example of this?

Thank you.
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

1. Because the components keep working together all the time. It wouldn't be necessary when you would 'manually' read the data (looping through the resultset) and write it to a stringgrid or some simple edits yourself. In that case there's no need to keep the dataset open, but you also don't have the database related functionality the DB aware components offer.
eg. Without connection it would be impossible to insert records in a db grid.

2. The question mark is the way a parameter must be sent to the firebird API. The problem is you're executig 2 statements in one operation. Seems like separating 2 statements by a semicolon is not the way to do that with firebird. I'm not sure it's even possible. So you should first execute the create table statement (using ExecSQL), then SQL.clean, add the insert statement+param value and execsql again.

3. Examples/simple or examples/components should do. But I must admit I didn't compile and test them for a long time now.

Mark
Image
littlebigman
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: 15.05.2009, 14:33

Post by littlebigman »

Thanks a lot Mark. I'm still getting an error with INSERT, but it looks like it's a Firebird issue, not ZeosLib so I'll resume reading "The Firebird Book" to learn more:
Project Project1.exe raised exception class EZSQLException with message 'SQL Error: validation error for column ID, value "*** null ***". Error Code: -625. The insert failed because a column definition includes validation constraints. The SQL: INSERT INTO mytable (name) VALUES (?); '.


SQL Error: validation error for column ID, value "*** null ***". Error Code: -625. The insert failed because a column definition includes validation constraints. The SQL: INSERT INTO mytable (name) VALUES (?);
And I'll take another look at the simple/ and components/ examples to see if I can whip up the complete application I need to write.
Post Reply