Page 1 of 1

Delphi & Firebird and Create Database

Posted: 22.04.2012, 18:41
by dve83
Hi guys,

tried searching through the forum and other places but havewnt been able to answer this questions.

I'm trying to connect to a Firebird (32bit) database through ZeosLib.

Basic code looks like this (notice the commented code);
procedure TForm1.FormCreate(Sender: TObject);
begin
ZConnection1.Database := 'c:\test.fdb';
ZConnection1.Protocol := 'firebird-2.5';
ZConnection1.User := 'SYSDBA';
ZConnection1.Password := 'masterkey';
{ ZConnection1.Properties.Add ('CreateNewDatabase=CREATE DATABASE ' +
QuotedStr ('c:\test.fdb') + ' USER ' +
QuotedStr ('sysdba') + ' PASSWORD ' + QuotedStr ('masterkey') +
' PAGE_SIZE 4096 DEFAULT CHARACTER SET ISO8859_1');}
ZConnection1.Connect;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
ZConnection1.Disconnect;
end;
If the database does not exists, and i uncomment the above code (to create the db), all works fine. If I keep it like that and then compile again, the app tries to create an already existing database and gives me the following error (does receive a database handle from the function isc_dsql_execute_immediate).
---------------------------
ptest
---------------------------
SQL Error: invalid database handle (no active connection). Error Code: -904. Unsuccessful execution caused by an unavailable resource. The SQL: CREATE DATABASE 'c:\test.fdb' USER 'sysdba' PASSWORD 'masterkey' PAGE_SIZE 4096 DEFAULT CHARACTER SET ISO8859_1;
---------------------------
OK
---------------------------
I can see that the db creation fails the second time around and that I dont receive a db handle. This raises the exception.

Is there a way to determine if the db already exists on the remote firebird server? (im testing locally, but plan to run software on remote server). Or is there perhaps a syntac for the above CreateNewDatabase parameter, that will allow it to check for the existing db before trying to create it.

Version used:
Firebird 2.5
Delphi XE
zeoslib from svn trunk/source

Many thanks for your replies / help
dve83

Posted: 22.04.2012, 19:26
by EgonHugeist
dve83,

that's a good question. I know you can ask the FB-Server for 'select count(*) from RDB$DATABASE' if that count > 0 then the DB exists. But this is not an answer for your Question. And is impossible without login.

This is a leak i know about. A dirty workaround would be:

try
ZConnection1.Properties.Add ('CreateNewDatabase=CREATE DATABASE ' +
QuotedStr ('c:\test.fdb') + ' USER ' +
QuotedStr ('sysdba') + ' PASSWORD ' + QuotedStr ('masterkey') +
' PAGE_SIZE 4096 DEFAULT CHARACTER SET ISO8859_1');}
ZConnection1.Connect;
exept
ZConnection1.Properties.Clear;
ZConnection1.Connect;
end;

It would be fine to Initialize the Driver an check the existing Databases with sysdba rights in this case. But what about security? Other engines are more flexible in this case.
Just look here http://zeos.firmos.at/viewtopic.php?t=3485

Or you write an DBExists parameter to an ini-file.

Btw. i saw you're creating the DataBase with ISO8859_1. If you plan to use this native ANSI characterset as client-connection characterset then please switch to:
http://zeos.firmos.at/viewtopic.php?t=3427

In my branch this is supported.

Best regards,

Michael

Posted: 22.04.2012, 20:04
by dve83
hi and thank you for the quick reply.

Yes, kind of got to the same conclusion (try and except - im testing for error -904 - invalid database handle)

ive tried to do some homework on the source, but couldn't find any quick access to the "open" function. I figure I could inherited from TZAbstractConnection and rewrite the Connect method, then again inherited from that like they did with TZConnect. Just didnt want to do too much until Ive found some perspectives from more seasoned developers using Zeoslib.

Not quite sure about the charset here. Still an example app Im busy with. What would you recommend as standard charset for Delphi and Firebird? Or perhaps direct me to some sources on charsets.

One more question: what is the process of becoming involved in the development of zeoslib? Might look into that as I go along - will check the website though.

kind regards

Posted: 23.04.2012, 20:22
by EgonHugeist
dve83,
ive tried to do some homework on the source, but couldn't find any quick access to the "open" function. I figure I could inherited from TZAbstractConnection and rewrite the Connect method, then again inherited from that like they did with TZConnect. Just didnt want to do too much until Ive found some perspectives from more seasoned developers using Zeoslib.
Hopefully you get help. I'm sure there is a way around. But i never tried it. :(
I know the 6.6.6 Components should support this. But i'm not sure for FB. So i think there was a reason to remove this option.

Mayby somebody of our users know an answer here?!
Not quite sure about the charset here. Still an example app Im busy with. What would you recommend as standard charset for Delphi and Firebird? Or perhaps direct me to some sources on charsets.
One reason for my workout and the branch i'd pointed you, was firebird. Which is my prefered of the free engines. Just read the threads here in the FireBird-Forum or on Alpha7 there where some problems specialy with CharacterSets. I'm hopefull to solved all this Bugs for the Full-Unicode-IDE's...

You'll find a lot of sources concerning CharacterSets in my branch in the PlainDrivers, or some functions in the ZCompatiblitity.pas. The other branches are the native workouts.. It's your choise, dont hesitate if you doubt to use it.
One more question: what is the process of becoming involved in the development of zeoslib? Might look into that as I go along - will check the website though.
This is quiet simple i think. Just help with some answers. Much better would be to present some suitable patches/fixes/proposals for a while. Test the sources and help us here. That was the way i joined to the team.

The first i got was commit right's for the svn-reps...

Best regards

Michael

Posted: 28.04.2012, 12:29
by amarildolacerda
There are no way to connect to firebird without an exists database. Everything firebird need to run are in one file. After 2.0 they include some security apart, but as a known, there are no way to access or link user to database.

You can use except to do that or test with fileexists(...), before connect. No other way that I known.