Databases in Lazarus/de

From Lazarus wiki
Revision as of 14:57, 4 May 2011 by Billyraybones (talk | contribs) (translated (part one))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Deutsch (de) | English (en) | español (es) | italiano (it)

Artikel über Datenbank-Programmierung

  • Lazarus-Datenbank-Tutorial - Eine Einführung über die Verwendung von Lazarus mit Datenbanken.
  • Datenbanken - Allgemeine Informationen über die Einsatzmöglichkeiten von Lazarus und FPC mit Datenbanken.
  • Datenbanken mit Lazarus - liefert einen allgemeinen Überblick über die Verwendung von Datenbanken mit Lazarus.
  • How to write in-memory database applications in Lazarus/FPC - Praktische Anweisungen zum programmieren mit MemDatasets
  • MySQLDatabases/de - erklärt, wie man auf eine einfachere Weise eine Verbindung mit einem MySQL Server herstellt.
  • Zeos_tutorial/de - Erzeugen von Datenbank-Anwendungen mit den Zeos-Komponenten.
  • Zeos+SQLite Tutorial - Gutes Tutorial mit Screenshots und Screencasts es wird auf spanisch erläutert, wie SQLite und Zeos, verwendet werden.
  • Pascal Data Objects is a database API that worked for both FPC and Delphi and utilises native MySQL libraries for version 4.1 and 5.0 and Firebird SQL 1.5, and 2.0. It's inspired by PHP's PDO class.
  • Lazarus Tdbf Tutorial/de - Datenbank-Entwicklung unter Verwendung der TDbf-Komponente (entwickelt von Micha Nelissen) mit Lazarus
  • Lazarus DB FAQ (deutsch) - Häufig gestellte Fragen zur Datenbank-Programmierung mit Lazarus.
  • Firebird - Firebird zusamen mit FPC/Lazarus verwenden
  • ODBCConn - Verwendung von 'ODBCConn' unter FPC/Lazarus für Verbindungen zu ODBC-Datenquellen.
  • MS Access - Zugriff auf eine MS-Access Datenbank

Ziel

Diese Seite liefert einen allgemeinen Überblick über die Verwendung von Datenbanken in Lazarus. Sie soll auch eine Seite sein, von der aus andere Datenbankseiten erreicht werden können.

Datasets

Die Verwendung von Datenbanken in Lazarus (oder FreePascal) basiert hauptsächlich auf der Klasse 'TDataset'. Diese repräsentiert für Ihre Anwendung eine Tabelle oder Abfrage. Aber wie bei vielen anderen fundamentalen Klassen, benutzen Sie nicht die Klasse 'TDataset' selbst, sondern einen Nachkommen von ihr. Davon gibt es viele. Sie bieten Zugriff auf unterschiedliche Arten von Datenbanken, sowohl auf lokale Datenbanken und Textdateien als auch auf Serverdatenbanken wie PostgreSQL, Firebird, MySQl und so weiter. Einige TDataset-Abkömmlinge linken direkt auf Datenbanktabellen, während andere dafür zusätzliche Komponenten oder Bibliotheken benötigen.

Die Seite Databases/de dokumentiert solche Nachkommen.

Dataset-Nachkommen die nichtvisuelle Komponenten sind, finden Sie (normalerweise) als Teil der Free Component Library (FCL), seltener in der Lazarus Component Library (LCL).

Verwendung von Datasets

Sie können Datasets programmatisch oder auch mit visuellen Steuerelementen verwenden. Eine typische Lazarus-Datenbankanwendung benutzt oft beide Verfahren. In jedem Fall ist der erste Schritt das Erzeugen eines TDataset-Nachkommen, ihn zu initialisieren für eine Verbindung mit der gewünschen Tabelle oder Abfrage, und diese zu öffnen. Dies kann entweder im Code zur Laufzeit geschehen oder Sie geben eine Komponente auf Ihr Formular und stellen deren Eigenschaften zur Entwurfszeit ein. Die Details dazu unterscheiden sich erheblich (abhängig von den verschiedenen TDataset-Nachkommen), schauen Sie also unter Databases/de nach, was für Ihre jeweilige Datenbank gemacht werden muss.

Wenn das Dataset geöffnet ist, wird eine Anzahl von Feldkomponenten erzeugt, eine für jedes Feld oder jede Spalte Ihrer geöffneten Tabelle oder Abfrage. Jede Feldkomponente ist ein Nachkomme von TField, passend für den besonderen Datentyp des Feldes, z.B. TStringField.

Verwendung von Datasets aus dem Code heraus

Programmatic access will be explained in more detail in Using Dataset and Field components, but as a very simple overview:

  • Use the TDataset descendant to open the table or query, filter the rows you want to see, and to move from row to row.
  • Use the TField descendants to:
    • Access general information about fields
    • Access the specific data values for the current row. (use the As... properties, such as AsString, AsInteger, etc.)
  • Access the fields of a TDataset descendant by using either:
    • The fields property, eg Fields[0] is the first field,
    • The FieldByName method, eg FieldByName('AGE') returns the field associated with the database field called 'AGE'

Verwendung der visuellen (datenabhängigen) Steuerelemente

To use databases in a simple, "RAD" style Lazarus application, you usually configure the dataset descendant at design time and the use the data-aware controls. To do this:

  • Add the dataset descendant for the database of your choice, together with any supporting components, to your form, and open it (Set the 'Active' property to true)
  • Add a TDatasource component (from the "Data Access" tab) to the form, and "link" it to the dataset (set the DataSet property)
  • Add data-aware controls from the "Data Controls" tab to the form, and link each one to the DataSource (not dataset) component
  • Most controls link to a single field, so you also need to set the Field for each tab.

See #Data Controls below for more details on the controls

Dataset-Status

Datasets can be in a number of states. While there are quite a few (look up TDataSetState in the source), the main ones to be aware of initally are

State Function
dsInactive The dataset is closed
dsBrowse The user can browse through the dataset, looking at values
dsEdit The user can edit values on the current row. Values are not saved until a post is performed.
dsInsert A new row has been added, and the user can set the values. The record is not saved until a post is performed

The other states are fairly transitory, and are usually handled "automatically". They are used internally and in more complicated code. If your database only views data, and you open the dataset at design time, you can largely ignore the state, as it will mostly be dsBrowse. However, most applications will want to change the data at some stage. If you are using data-aware controls, they will handle a lot of this automatically. If you change the text in a DBEdit control, for example, it will put the dataset into dsEdit state - unless you are already in dsEdit or dsInsert. If you "scroll" to a different record while the current state is dsEdit or dsInsert, that record will be "posted" and the dataset revert to dsBrowse. However, if you are accessing the dataset from code, you will often have to change the state in code as well. The dbNavigator control (see below) allows the user to change the state explicitly.

Senden und Abbrechen (Post and Cancel)

If you have edited or inserted a record, the new values are held in a buffer.

  • Calling the dataset cancel method removes the new record (insert) or reverts the values to their previous values (edit).
  • Calling the dataset post method saves the values (edit) or record (insert). In some dataset descendants, they will be written to the database immediately, while in others they will be stored in a list of updates until a further call is made to save all changes to the database. Finally, even when they are written to the database, you may still have to call a "commit" method to make the database write them permanently. All of this also varies considerably with the dataset descendant, so look up the details for the one you are using.

Datensteuerelemente

To use any of these controls, add the control to a form and set at least the datasource property. Other key properties will be noted.

Steuerelemente für einzelne Felder

These controls all attach to a single field. As well as datasource, set the field name. Controls include:

DBGrid control

This control can show a number of fields in a row/column layout - in fact by default it shows them all. However, you can put entries into the columns collection to restrict it to specific fields and to set the widths and titles of each column.

There seem to be some issues currently with editing in dbGrid, so while editing is certainly possible, it may be safer to use it as display only (set readonly to true) and provide single field controls for editing.

Navigator Control

This control gives the user some direct control over the dataset. It allows the user to:

  • Move to the next or previous record, or to the start or end of the records
  • Add a new record (equivalent to a dataset.insert method call)
  • Put the dataset into edit mode
  • Delete a record
  • Post or Cancel current changes
  • Refresh the data (useful in multiuser database applications)

Key Properties:

  • VisibleButtons: Lets you control what the user can do. For example, if deletes are not allowed, hide the delete button. If you have a DBGrid attached to the same dataset, you may decide you do not need the next and prior buttons.
  • Width: If you do not show all buttons, you may want to set the width to (height*number_of_visible_buttons)