Difference between revisions of "Databases/ru"

From Lazarus wiki
Jump to navigationJump to search
Line 86: Line 86:
 
**Метод FieldByName, как пример FieldByName('AGE') вернёт поле, которое в базе данных называется 'AGE'.
 
**Метод FieldByName, как пример FieldByName('AGE') вернёт поле, которое в базе данных называется 'AGE'.
  
See [[Database_field_type]] for a list of field types.
+
См. [[Database_field_type|Тип поля базы данных]] для просмотра списка типов полей.
  
 
===Использование визуальных (db-ориентированных) компонентов===
 
===Использование визуальных (db-ориентированных) компонентов===

Revision as of 14:10, 21 October 2019

Deutsch (de) English (en) español (es) français (fr) italiano (it) 日本語 (ja) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN)

Databases portal

References:

Tutorials/practical articles:

Databases

Advantage - MySQL - MSSQL - Postgres - Interbase - Firebird - Oracle - ODBC - Paradox - SQLite - dBASE - MS Access - Zeos

Эта страница является введением в темы Lazarus и database. В следующей таблице представлен обзор поддерживаемых баз данных.

Должны быть установлены только компоненты базы данных, для которых есть клиентские библиотеки (если базе данных нужны клиентские библиотеки), иначе Lazarus может не запуститься из-за отсутствующих файлов. Затем Lazarus необходимо переустановить, так как удаление компонента невозможно.

Поддерживаемые базы данных

База данных Имя пакета Нужна клиентская библиотека? Нужен сервер? Поддерживаемые версии Поддерживаемые платформы
Advantage TAdsDataSet Да Нет 10.1 и больше i386: Linux, Win32
DBase DBFLaz Нет Нет III+, IV, VII Все
TurboPower FlashFiler FlashFiler Нет Нет - Win 32, (win64?)
In memory memds Нет Нет - Все
In memory bufdataset Нет Нет - Все
Firebird SQLdb Да Зависит от1 1 - 2.5 i386: Linux, Win32
(Visual) FoxPro DBFLaz Нет Нет 2.0, 2.5, 3.0 (не полностью) Все
Interbase SQLdb Да Да 4 - 6 i386: Linux, Win32
Microsoft SQL Server SQLdb Да Да 6- FPC 2.6.2+. Linux, OSX, Win32, возможно *BSD, возможно Solaris2
MySQL SQLdb Да Да 3.0 - 5.5 i386: Linux, Win32
ODBC SQLdb Да Зависит от3 3.x i386: Linux, Win32
Oracle SQLdb Да Да - -
Paradox TParadoxDataSet Нет Нет до уровня таблицы 7 (и выше ??) Все
Paradox TParadox Да Нет Win32
PostgreSQL SQLdb Да Да 6.6 - 8 i386: Linux, Win32
Sybase Adaptive Server Enterprise (ASE) SQLdb Да Да Любая Linux, OSX, Win32, возможно *BSD, возможно Solaris2)
SQLite SQLdb Да Нет sqlite3 Все
SQLite SQLite(3)Laz Да Нет sqlite2,sqlite3 Все
Text files sdf Нет Нет - Все

Примечание (1): Вы можете использовать встроенную версию Firebird для Windows и Linux (возможно, также для Mac OS X) или подключиться к серверу Firebird, работающему на Windows/Unix/OSX/FreeBSD/других платформах, поддерживаемых Firebird.

Примечание (2): Эти коннекторы используют библиотеку FreeTDS в качестве драйвера. Документация FreeTDS указывает, что она должна быть построена как минимум на этих платформах. Версии Windows для x86 и x64 можно загрузить, например, отсюда отсюда(х32) и отсюда(х64)

Примечание (3): Этот номер версии относится к стандарту ODBC, а не к номеру версии драйвера или диспетчера драйверов. Для большинства СУБД существуют драйверы ODBC 3.x.

Привязка к клиентским библиотекам баз данных

Если вы хотите использовать одну из баз данных, которая требует клиентских библиотек, эти библиотеки должны быть установлены. Не только на компьютере, на котором вы программируете, но и на компьютерах, на которых должно работать приложение. Обратите внимание, что некоторые базы данных (в частности, MySQL) работают только в том случае, если привязки, скомпилированные в приложении, имеют ту же версию, что и у установленных библиотек. Вы можете узнать, как установить эти библиотеки (.so файлы в системах * nix и .dll в Windows) на веб-сайте разработчиков баз данных. Модули привязки можно найти в каталоге packages/base в fpc-sources. Они в основном состоят из клиентских API-вызовов, таких как mysql_connect_database, которые совершенно разные для каждой базы данных. Можно писать приложения баз данных, используя эти модули, но обычно это гораздо более трудоемко и критично к ошибкам, чем использование компонентов DB-модуля Lazarus.

Большинство из этих пакетов привязок жестко связаны с клиентскими библиотеками. Это означает, что, если приложение скомпилировано с одним из этих модулей, все приложение не может быть связано, если клиентские библиотеки недоступны на рабочей станции. В свою очередб, это означает, что исполняемый файл вашей программы не будет сгенерирован, если на вашем компьютере не установлен, например, клиент MySQL, и вы используете в своей программе модуль mysql4.pp. Если вам удастся скомпилировать программу на компьютере, на котором установлены клиентские библиотеки MySQL, она все равно не запустится ни на одном другом компьютере без соответствующих клиентских библиотек MySQL. Другими словами: для этих баз данных вам нужно установить клиентские библиотеки на компьютере разработчика и установить эти клиентские библиотеки вместе с вашим приложением.

Чтобы избежать таких проблем, некоторые пакеты также могут динамически подключаться к библиотекам. Перед выполнением каких-либо вызовов этих библиотек устройство должно быть 'инициализировано'. Эта инициализация завершается ошибкой, если на компьютере не установлен клиент базы данных. Если программа готова использовать клиентскую библиотеку, модуль должен быть 'освобожден'.

Наборы данных (Dataset)

Базы данных в Lazarus (или FreePascal) основывают свою работу на базовом классе TDataset. Этот класс представляет таблицу или результат запроса в Вашем приложении. Как и многие другие базовые классы, Вы не можете использовать TDataSet в своём приложении непосредственно, а используете только его потомков, которых довольно много. Они обеспечивают доступ к различным видам баз данных, таким как локальный dbase или текстовые файлы, а так же серверами баз данных, таких как PostgreSQL, Firebird, MySQl и т.п. Некоторые потомки TDataSet связываются непосредственно с таблицами базы данных, в то время как другие используют дополнительные компоненты или библиотеки.

Обратитесь к странице базы данных для получения более полной информации об этом.

Потомки Dataset, будучи не визуальными компонентами, обычно являются частью свободной библиотеки компонентов (FCL), а не библиотеки компонентов Lazarus (LCL).

Использование наборов данных в коде программы

О программном доступе объясняется более подробно в "Использование компонентов Dataset и Field", вот краткий обзор:

  • Используйте потомок TDataset чтобы открыть таблицу или запрос, отфильтруйте те строки, которые хотите видеть и двигайтесь от строки к строке.
  • Используйте потомок TField для:
    • Доступа к общей информации о поле.
    • Доступа к данным текущей строки. (используя свойство AsХХХХ, например AsString, AsInteger и так далее.)
  • Используйте для обращения к полям потомка TDatasetтак же:
    • Свойство Fields, к примеру Fields[0] для самого первого поля,
    • Метод FieldByName, как пример FieldByName('AGE') вернёт поле, которое в базе данных называется 'AGE'.

См. Тип поля базы данных для просмотра списка типов полей.

Использование визуальных (db-ориентированных) компонентов

Чтобы использовать базы данных в простом, приложении Lazarus в "RAD"-стиле, обычно настраивается потомок TDataSet во время разработки и добавляются специализированные компоненты. Например:

  • Добавьте потомка dataset для выбранной Вами базы данных вместе с другими необходимыми компонентами на Вашу форму и откройте его (Установите свойство 'Active' в True).
  • Добавьте на форму компонент TDatasource (с вкладки "Data Access") и свяжите его с датасетом (установив свойство DataSet).
  • Добавьте компоненты отображения данных с вкладки "Data Controls" и свяжите их с компонентом DataSource (не dataset).
  • Большинство компонентов связываются только с одним полем, поэтому Вам надо будет установить свойство Field для каждого такого компонента.

Смотрите #Data Controls ниже.

Рабочие состояния (Статусы) Dataset

Наборы данных могут быть в нескольких состояниях (Статусах), которые отображаются в свойстве State. Вот какие состояния могут быть (смотрите TDataSetState в исходниках):

State Описание
dsInactive Набор данных закрыт, доступ к данным невозможен
dsBrowse В этом состоянии можно просматривать набор данных, искать какие-либо значения
dsEdit В этом состоянии можно редактировать данные в текущей строке. Изменённые значения не будут записаны, пока не применён метод Post.
dsInsert В этом состоянии только что вставлена новая строка данных и туда можно заносить новые значения. Строка не будет записана, пока не применён метод Post.

Другие состояния обычно являются кратковременными и обрабатываются автоматически. Они используются внутри компонента и более сложны в коде обработки. Если Ваше приложение только просматривает данные и Вы открываете набор данных во времени разработки, то Вы можете просто проигнорировать статус, поскольку он главным образом будет dsBrowse. Однако большинство приложений будут тем или иным образом всё-таки изменять данные. Если Вы используете DB-ориентированные компоненты, то они обрабатывают эти состояния автоматически. Если Вы измените текст в компоненте (к примеру) TDBEdit, то он пометит набор данных в состояние dsEdit, если только Вы уже не поставили вручную статус в dsEdit или dsInsert. Если Вы будете переходить от одной записи к другой, то та запись, которая была в состоянии dsEdit или dsInsert, автоматически выполнит метод Post, при переходе на новую строку и DataSet вернётся в состояние dsBrowse. Однако если Вы обращаетесь к набору данных в коде, то его состояние Вы должны выставлять вручную. Компонент TDBNavigator позволяет изменять статус самому пользователю.

Dataset UpdateStatus

UpdateStatus determines the current state of the record buffer, if updates have not yet been applied to the database.

Example how to detect if ApplyUpdates will Insert, Update or Delete data:

procedure QueryAfterPost(DataSet: TDataSet);
begin
  case DataSet.UpdateStatus of
    usUnmodified : ShowMessage('Unmodified');
    usModified   : ShowMessage('Modified');
    usInserted   : ShowMessage('Inserted');
    usDeleted    : ShowMessage('Deleted');
  end;
end;
Value Explanation
  • usUnmodified: Record is unmodified
  • usModified: Record exists in the database but is locally modified
  • usInserted: Record does not yet exist in the database, but is locally inserted
  • usDeleted: Record exists in the database, but is locally deleted

Принятие и отмена изменения

Если Вы редактируете или вставляете новую запись, то значения этой записи сначала помещаются в буфер. После этого:

  • Вызов метода Dataset.Cancel удаляет новую запись (при вставке) или или возвращает старые значения записи (при редактировании).
  • Вызов метода Dataset.Post сохраняет значения (при редактировании) или новую запись (при вставке).

В некоторых потомках Dataset значения будут немедленно написаны базе данных, в то время как в других они будут сохранены во временной таблице обновлений, пока не будет вызван некий окончательный метод, чтобы сохранить все изменения в базе данных. Однако иногда, даже когда значения записаны в базу данных, Вам, вероятно, придется дополнительно вызвать метод "Commit", чтобы заставить базу данных принять эти изменения. Всё это, в значительной степени, зависит от конкретного потомка DataSet'а, поэтому за подробностями обращайтесь к описанию этого самого конкретного потомка.

Inserting a new record

To insert a new record into a TDataset descendent, one should use the method Insert. After that one can set the field values and then finally call Post to commit the new record, as the example below shows.

The example also shows how to insert BLOB data from a file - you can also use LoadFromStream to load the data from a stream.

  MyDataset.Insert;
  MyDataset.Fields[0].AsInteger := 4; //an integer field
  MyDataset.Fields[1].AsString := 'First Name'; //a string field
  TBlobField(MyDataset.Fields[2]).LoadFromFile('SomeBlobfile.bin'); //blob field
  MyDataset.Post;

How to quickly jump to 1 particular record in the table

After selecting all records of the table

If you use SELECT * FROM to select all records of the table and then desires to quickly jump between them, you will have to build an index and search on it. It is more efficient to select only the record that you want.

Selecting only the desired record

One fast solution to jump to a particular record is to select only it, for example doing:

var
  MyDataset: TSQLQuery;
begin
  //...
  MyDataset.FieldDefs.Add('SessionId', ftLargeint);
  MyDataset.FieldDefs.Add('GameEvent', ftLargeint);
  MyDataset.FieldDefs.Add('TableId', ftInteger);
  MyDataset.FieldDefs.Add('LoggedIn', ftBoolean);
  MyDataset.FieldDefs.Add('PlayerId', ftInteger);
  MyDataset.Active := False;
  { Non-parameterized format; may run into issues with text containing ' and dates
  SQLText := Format('select * from "GameSession" WHERE "SessionId"=%d', [ASessionId]);
  }
  // Solution: parameterized query:
  // Actually, if this is done in a loop, you only need to set the SQL.Text once,
  // and only change the parameter value
  MyDataset.SQL.Text := 'select * from "GameSession" WHERE "SessionID"=:SessionID');
  MyDataSet.ParamByName('SessionID').AsLargeInt := ASessionID;
  try
    MyDataset.Active := True;
  except
    //...
  end;

You can then read information using something like this:

 lPlayerId := MyDataset.Fields[4].AsInteger;

Filtering

You can filter your dataset to restrict the records to a subset you want (e.g. all surnames starting with Smith).

Locate/lookup

Although more often used in non-SQL datasets (e.g. TParadoxDataSet, TDbf) you can jump between records using locate/lookup.

Using TSQLQuery

For more information about TSQLQuery see Working With TSQLQuery

Exporting

FPC/Lazarus contains functionality to let you export datasets to various formats; see e.g.

Of course, you could also do it manually (see e.g. FPSpreadsheet#Converting_a_database_to_a_spreadsheet for export to Excel format using fpspreadsheet)

Data Controls

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.

Datasource Control

This control keeps track of which record the linked controls currently are on. The datasource contorl must be linked to a dataset (e.g. a TSQLQuery).

Single Field Controls

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

  • DBText control Displays a text field (readonly, no border)
  • DBEdit control Displays / edits a text field as an edit box
  • DBMemo control Displays / edits a text field in a multi-line edit box
  • DBImage control Displays a picture stored in a database as a BLOB. Note: by default, Lazarus stores a header with the image type before the image data in the database BLOB field. This is different from Delphi. However, you can make TDBImage Delphi compatible: see Lazarus For Delphi Users#TDBImage
  • DBListBox control and DBComboBox Control Allow the user to insert values into a database field from the list of values in the Items property of the controls
  • DBLookupListBox control and DBLookupComboBox control, see also TDBLookupComboBox Allow the user to insert values into a database field by displaying the contents of a lookup field in another table. Though these controls store their results in a single field, you need another field for the lookup values. Note: at lest for DBLookupComboBox, there is a bug with FPC 2.6.0 that requires the listfield to be present in the datasource as well, you can bypass it by declaring a calculated field with the same name as the listfield in the datasource's dataset that does nothing.
  • DBCheckBox control Displays / edits a boolean field by checking/clearing a check box
  • DBRadioGroup control Displays the items as in a normal radio group, reading/setting the field value from a matching values list
  • DBCalendar control Displays / edits a date field using a calendar panel
  • DBGroupBox control

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.

Apart from the mentioned documentation, some more detail can be found here: Grids Reference Page#TCustomDBGrid

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)

Выполнение тестов базы данных FPC

Free Pascal database components include a fpcunit-based test framework, dbtestframework, that can be used to verify functionality. See the directory source\packages\fcl-db\tests\ in your FPC source tree. Included is a test framework that can be run on various database components, as well as some other tests (e.g. test of database export).

To run the test framework on a certain database:

1. Save source\packages\fcl-db\tests\database.ini.txt as source\packages\fcl-db\tests\database.ini

2. Modify source\packages\fcl-db\tests\database.ini to choose which database type you will use.

Example for Interbase/Firebird:

[Database]
type=interbase

3. In the same file, customize settings for your database. E.g. if you chose interbase before:

[interbase]
connector=sql
connectorparams=interbase
; Database name/path (note: database needs to exist already)
; You can use aliases (see aliases.conf in your Firebird documentation)
name=testdb
user=sysdba
password=masterkey
; your hostname may very well differ:
; Leave blank if you want to use an embedded Firebird database
hostname=192.168.0.42

4. Compile and run source\packages\fcl-db\tests\dbtestframework.pas (You can also use Lazarus to compile and run the GUI version, dbtestframework_gui) If you are using an embedded database on Windows (e.g. Firebird embedded or sqlite), copy the required DLL files to the directory first. The output will be in XML format (or displayed on your screen if you use dbtestframework_gui).

Please see source\packages\fcl-db\tests\README.txt for more details.

Database packages contained in Lazarus

sqldblaz.lpk

sqldbcomponents.png

This package provides access to different databases. These include:

  • Interbase/Firebird
  • Microsoft SQL Server (except on Lazarus/FPC x64 for Windows)
  • MySQL
  • Oracle (except on Lazarus/FPC x64 for Windows)
  • PostgreSQL (except on Lazarus/FPC x64 for Windows)
  • SQLite (with support for the Spatialite extension)
  • Sybase ASE (Adaptive Server Enterprise - not to be confused with Sybase ASA) (except on Lazarus/FPC x64 for Windows)
  • any database that has an ODBC driver.

The components (TSQLQuery, TSQLTransaction, TIBConnection, TODBCConnection, TOracleConnection, TMSSQLConnection, TMySQL40Connection, TMySQL41Connection, TMySQL50Connection, TPQConnection, TSybaseConnection) are on the 'SQLdb' tab in the component palette.

dbflaz.lpk

This package provides access to dBase and FoxPro databases. You can get more information in the Lazarus Tdbf Tutorial. The TDbf component is on the Data Access tab in the component palette.

sqlitelaz.lpk

This package provides access to SQLite databases. You can get more information in the Lazarus Database Overview.

sdflaz.lpk

The component TSdfDataSet can be found on the Data Access tab in the component palette.

lazreport.lpk

The homepage of the report generator is http://lazreport.sourceforge.net/. More informationen (et al. an additional link) can be found here. LazReport depends on the Printer4Lazarus package. With revision 11950 LazReport was included in the Lazarus SVN repository.

lazdbexport.lpk

See lazdbexport.

External packages / libraries

Zeos DataBase Objects

These components provide access to different databases. You can find more information here. This wiki also contains a Zeos tutorial.

Pascal Data Objects

There is now an alternative.

Support:

  • MySQL 4.1 and 5.0
  • sqlite-2 and sqlite-3
  • pgsql-8.1
  • interbase-5, interbase-6, firebird-1.0, firebird-1.5, firebird-1.5E, firebird-2.0, firebird-2.0E
  • mssql (Microsoft library) and sybase (FreeTDS library)
  • oracle

like prepared statements, binding, and stored procedures are supported by database API called Pascal Data Objects, which is inspired by PHP Data Objects. All the code and documentation necessary to use this new API is available on Sourceforge:

http://pdo.sourceforge.net

TPSQL

These components provide access via TCP/IP to PostgreSQL databases. You can find more information on this page.

FIBL

These components provide access to Interbase and Firebird databases. The homepage is http://sourceforge.net/projects/fibl.

IBX

IBX For Lazarus are components to access Firebird databases: see IBX

FBLib Firebird Library

FBLib is an open Source Library No Data Aware for direct access to Firebird Relational Database from Borland Delphi/Kylix, Free Pascal and Lazarus.

Current Features include:

  • Direct Access to Firebird 1.0.x, 1.5.x and 2.x Classic or SuperServer
  • Multiplatform [Win32,Gnu/Linux,FreeBSD)
  • Automatic select client library 'fbclient' or 'gds32'
  • Query with params
  • Support SQL Dialect 1/3
  • LGPL License agreement
  • Extract Metadata
  • Simple Script Parser
  • Only 100-150 KB added into final EXE
  • Support BLOB Fields
  • Export Data to HTML SQL Script
  • Service manager (backup,restore,gfix...)
  • Events Alerter

You can download documentation on FBLib's website.

Unified Interbase

UIB provides access to Interbase, Firebird and YAFFIL databases. The homepage is www.progdigy.com. A svn repository is available under https://uib.svn.sourceforge.net/svnroot/uib .

TechInsite Object Persistence Framework (tiOPF)

More information about tiOPF can be found on this page.

Advantage TDataSet Descendant

The Advantage TDataSet Descedant provides a means of connecting to (and opening tables with) the Advantage Database Server. Advantage is a flexible, administration-free embedded database that provides Client/Server as well as Peer-to-peer access to Clipper, FoxPro and Visual FoxPro 9 DBF file formats, as well as a proprietary file format that provides a migration path allowing the use of newer features.

Key Features:

  • Royalty-free peer-to-peer database access with migration path to Client/Server
  • Multi-Platform (Clients supported on Windows and Linux, Server supported on Windows, Linux, and NetWare)
  • Supports Both navigational and relational SQL database access
  • Full-text search engine
  • Table, Index, Memo, and communication encryption
  • Compatible with native TDataset components
  • Online Backup
  • Server supports Replication

For more information, see the Advantage Database Server website.

ZMSQL, sql-enhanced in-memory database

For more information, see the ZMSQL wiki page

ZMSQL is an open source, TBufDataset descendant SQL enhanced in-memory database for Free Pascal (FPC), operating with semicolon-separated values flat text tables. Completely written in Pascal, it has no dependencies on external libraries. It uses JanSQL engine for SQL implementation.

It offers:

  • Loading from and saving to flat text tables
  • Use of SQL to query the data
  • Copy data and schema from other datasets
  • Option to predefine fielddefs or create it on-the fly
  • Master/detail filtering
  • Referential integrity
  • Parameterized queries

The download contains the source code, some demo applications illustrating the features of the component as well as a readme.

See also