Difference between revisions of "Lazarus Tdbf Tutorial/de"

From Free Pascal wiki
Jump to navigationJump to search
Line 83: Line 83:
  
 
     MyDbf.CreateTable;
 
     MyDbf.CreateTable;
 +
 +
=How to add indexes to a table=
 +
 +
If your database is larger than a few records, chances are you will want to have indexes defined to make searching faster. To change the index structure of a table, we will want to have exclusive access to the table - which we would have while creating it anyway.
 +
 +
        MyDbf.Exclusive := True;
 +
        MyDbf.Open;
 +
 +
Now, we just have to add the index.
 +
 +
        MyDbf.AddIndex('custid', 'Id', [ixPrimary, ixUnique]);
 +
        MyDbf.AddIndex('custname','Name', [ixCaseInsensitive]);
 +
        MyDbf.Close;

Revision as of 17:43, 6 September 2005

Überblick

This tutorial is about basic database development using the TDbf component (by Micha Nelissen) with Lazarus. Additional documentation for the TDbf is available. This page was created by Tony Maro but other contributions are welcome!

What you will need

No doubt this will soon be easier as FreePascal releases the next version 2.0, however currently you will need a recent CVS edition of FPC 1.9.X in order to properly use the TDbf component. It may be possible to download just the TDbf component by itself and use it with the 1.1 version of FreePascal, however this document was written with 1.9.X in mind, partly due to bug fixes in the other database components used in Lazarus.

You will also need to install the DbfLaz package that comes with Lazarus. It's located in the lazarus/components/tdbf/ directory.

What the TDbf provides

The TDbf provides access to dBase and FoxPro database tables for Lazarus (and others). It allows for reading, writing and creation of dBase III+, dBase IV, Visual dBase VII and FoxPro tables. It does all of this without the need for additional libraries or database engines. Simply drop the TDbf on your form and you have instant access to a cross-platform database environment. The TDbf works in both Windows and Linux using Lazarus.

How to create a new database table

As there is no "Database Desktop" application for Lazarus yet, we must create a new database in code.

Setting the path

It's a good idea to give your application's database it's own directory. This simplifies making backups of the data. There are two ways to set the path. You can set the full path using the FilePathFull property, or you can set a path relative to the current application path with FilePath. For instance, setting "FilePath" at runtime to "data/" would use a data subdirectory just below the executable file. Setting the "FilePathFull" property to "/var/data/" would place everthing in that exact folder, ignoring the application's location.

Choosing a TableLevel

By default, the TDbf will create dBase IV tables. While this is very compatible, there are features you may wish to use that are not supported. To support auto-incrementing fields, you must use something newer. The table types are:

  • 3 dBase III+
  • 4 dBase IV
  • 7 Visual dBase VII
  • 25 FoxPro

You choose a table type by setting the TableLevel property appropriately.

Adding fields

Creating fields for your new table at runtime pretty much follows the old Delphi standard. Once you have set your FilePath, TableLevel, and TableName properties, manipulate the FieldDefs property to set up the structure. For example:

MyDbf.FilePathFull := '/location/to/my/data';
MyDbf.TableLevel := 7;
MyDbf.TableName := 'customers.dbf'; // note: is the .dbf really required?
With MyDbf.FieldDefs do begin
  Add('Id', ftAutoInc, 0, True);
  Add('Name', ftString, 80, True);
End;

Field types are defined as:

  • ftUnknown
  • ftString
  • ftSmallInt
  • ftInteger
  • ftWord
  • ftBoolean
  • ftFloat
  • ftCurrency (TableLevel 25)
  • ftBCD (TableLevel 25)
  • ftDate
  • ftTime
  • ftDateTime
  • ftBytes (TableLevel 25)
  • ftVarBytes
  • ftAutoInc (TableLevel 7 or 25)
  • ftBlob
  • ftMemo
  • ftGraphic
  • ftFmtMemo
  • ftParadoxOle
  • ftDBaseOle
  • ftTypedBinary
  • ftCursor
  • ftFixedChar
  • ftWideString
  • ftLargeInt
  • ftADT
  • ftArray
  • ftReference
  • ftDataSet
  • ftOraBlob
  • ftOraClob
  • ftVariant
  • ftInterface
  • ftIDispatch
  • ftGuid
  • ftTimeStamp
  • ftFMTBcd

Bold types are currently supported

Go ahead and create it!

Once you have defined the fields you wish to use in your new table, you can go ahead and create it with:

   MyDbf.CreateTable;

How to add indexes to a table

If your database is larger than a few records, chances are you will want to have indexes defined to make searching faster. To change the index structure of a table, we will want to have exclusive access to the table - which we would have while creating it anyway.

       MyDbf.Exclusive := True;
       MyDbf.Open;

Now, we just have to add the index.

       MyDbf.AddIndex('custid', 'Id', [ixPrimary, ixUnique]);
       MyDbf.AddIndex('custname','Name', [ixCaseInsensitive]);
       MyDbf.Close;