Search code examples
delphiinterbase

Creating database in InterBase


I try to create a InterBase database on Delphi using TIBDatabase type.

ibdb:=TIBDatabase.Create(nil);
with ibdb do
  begin
    SQLDialect:=3;
    DatabaseName:=Self.name;
    Params.Clear;
    Params.Add('USER "SYSDBA"');
    Params.Add('PASSWORD "masterkey"');
    Params.Add('PAGE_SIZE 4096');
    LoginPrompt:=false;
    try
      CreateDatabase;
    except on E: Exception do
      ShowMessage('Can''t create database. '+E.Message);
    end;
  end;

Self.name is string like a 'localhost:C:\db.ib'. Code works without errors. Database file created. But IBConsole doesn't show this database on IB server. How can I check that database was added on server?


Solution

  • The following works for me and avoids all the problems you've mentioned so far:

    1 Start a new Delphi project

    2 Drop a TIBDatabase onto the form

    3 Add the following CreateDatabase procedure to the form

    procedure TForm1.CreateDatabase;
    begin
      IBDatabase1.SQLDialect:=3;
      IBDatabase1.DatabaseName := 'D:\aaad7\interbase\newdb.gdb';  //edit for
        //  your system & check that you have file-creation rights in the 
        //  database folder you want to use.
    
      IBDatabase1.Params.Clear;
      IBDatabase1.Params.Add('USER ''SYSDBA''');  // Note that this line
      //  contains 5 single-quotes and no double quotes
    
      //  Also note that the omission of the usual '=' between the 
      //  parameter name and value for the USER and PASSWORD is deliberate, 
      // because it's evidently required by the IB api
    
      IBDatabase1.Params.Add('PASSWORD ''masterkey''');  // Ditto
    
      IBDatabase1.LoginPrompt:=false;
    
      try
        IBDatabase1.CreateDatabase;
      except on E: Exception do
        ShowMessage('Can''t create database. '+E.Message);
      end;
    end;
    

    4 Add a TButton and set its OnClick event to call CreateDatabase.

    5 Compile & run and click Button1.

    Using Delphi 7 and Interbase XE7, this code successfully creates the newdb.gdb database and this database stays on disk after the Delphi application terminates. I can then open the database in IBConsole using the procedure described below. I cannot delete the databse file via the windows shell while IBConsole is running but I can after I close it.

    Do you get the same result and, if you do, does that solve your problem and if not, why not?

    Btw, I've written the CreateDatabase code to avoid the use of "with", because it usually creates more problems than it avoids and I loathe it, and to avoid the dynamic creation of the TIBDatabase component, because that serves no useful purpose.

    Notes

    1. Once you have created a database in code as above, there is no need to "attach" it to IBConsole.Exe or to the Interbase server in order to be able to use it.
      You could set up an IBDatabase, IBTransaction and IBQuery to connect to it, and create tables in it by issuing "CREATE TABLE ..." SQL statements to it from the IBQuery.

    2. If you want the database to be listed in the IBConsole utility, you can either

    a) Add it manually using the procedure in Previous Answer below or

    b) Add it in code to the IBConsole configuration file, IBConsole.XML, which you will find in C:\users[your user name]\Appdata\Roaming\Embarcadero\Interbase, using a Delphi XML access library of your choice. If you add it using the manual procedure below, then close IBConsole.Exe so that the config file is updated on disk, you'll be able to examine the XML file and find the node for the database under the Server/Databases node. It will look something like

    <key name="Newdb">
    <value name="Accessed">A32BFE2475B3E440</value>
    <value name="CaseSensitiveRole">0</value>
    <value name="CharacterSet"/>
    <value name="Created">7DB1327474B3E440</value>
    <value name="DatabaseFiles">D:\aaad7\Interbase\NEWDB.GDB</value>
    <value name="Encrypted">0</value>
    <value name="EUA Enabled">0</value>
    <value name="Password">096e6376786a78726d82</value>
    <value name="Role"/>
    <value name="Save DBAlias">1</value>
    <value name="SEP"/>
    <value name="Use DBAlias">0</value>
    <value name="Username">SYSDBA</value>
    <value name="UseSSL">0</value>
    </key>
    

    Previous answer

    Try this:

    • In IBConsole, click the Local Server node and select its Databases node

    • Right-click the Databases node and select Add

    • In the Add database and Connect pop-up, click the button to the RHS of the File: field

    • From there, you should be able to navigate to and select your new database.

    Tested with Interbase XE7 IBConsole.

    If that doesn't work for you, exactly which step fails and how?