| 1 | | - | ## 介绍 |
| 2 | | - | |
| 3 | | - | 本页面介绍了**Lazarus和数据库**。下表提供了所支持的数据库。 |
| 4 | | - | |
| 5 | | - | **警告:**你应该只安装已经安装过客户端的数据库组件(如果数据库支持客户端支持)。否则Lazarus可能无法启动,因为丢失文件。那么你将不得不重新安装Lazarus,因为你不能卸载组件。 |
| 1 | + | --- |
| 2 | + | [[Portal:Databases|**Databases portal**]] |
| 3 | + | References: · [[Databases|General info]] · [[Database libraries|Libraries]] · [[Database field type|Field types]] · [[Data Controls tab|Controls]] · [[Lazarus DB Faq|FAQ]] · [[SqlDBHowto|SQL how-to]] · [[Working With TSQLQuery]] · [[How to write in-memory database applications in Lazarus/FPC|In-memory database applications]] · Tutorials/practical articles: · [[Lazarus Database Overview|Overview]] · [[SQLdb Tutorial0|0 - Database set-up]] · [[SQLdb Tutorial1|1 - Getting started]] · [[SQLdb Tutorial2|2 - Editing]] · [[SQLdb Tutorial3|3 - Queries]] · [[SQLdb Tutorial4|4 - Data modules]] · [[SQLdb Programming Reference]] · Databases · [[Advantage Database Server|Advantage]] \- [[MySQLDatabases|MySQL]] \- [[mssqlconn|MSSQL]] \- [[postgres|Postgres]] \- [[Firebird|Interbase]] \- [[Firebird]] \- [[Oracle]] \- [[ODBCConn|ODBC]] \- [[TParadox|Paradox]] \- [[SQLite]] \- [[Lazarus Tdbf Tutorial|dBASE]] \- [[MS Access]] \- [[Zeos tutorial|Zeos]] |
| 4 | + | |
| 5 | + | 本文介绍了[[Lazarus]]和**数据库** 。下表给出了 Lazarus 支持的数据库。 |
| 6 | 6 | | |
| 7 | | - | ### 数据库支持 |
| 7 | + | 请只安装必要的数据库组件,如数据库需用到客户端库则应先安装客户端库。否则由于缺少文件,Lazarus 可能会无法启动。因为数据库组件无法卸载,一旦有问题 Lazarus 就得重装。 |
| 8 | 8 | | |
| 9 | | - | | 数据库 | 包名 | 需要客户端库? | 需要服务端? | 支持版本 | 支持平台 | |
| 10 | | - | |----|----|----|----|----|----| |
| 11 | | - | | [**Advantage**](http://www.advantagedatabase.com/) | TAdsDataSet | 是 | 否 | 10.1 及以上 | i386: Linux, Win32 | |
| 12 | | - | | **DBase** | [[Lazarus Tdbf Tutorial/zh CN|DBFLaz]] | 否 | 否 | III+, IV, VII | 所有 | |
| 13 | | - | | **In memory** | memds | 否 | 否 | \- | 所有 | |
| 14 | | - | | **In memory** | bufdataset | 否 | 否 | \- | 所有 | |
| 15 | | - | | [**Interbase**](http://www.embarcadero.com/products/interbase-smp) | [[Firebird|SQLdb]] | 是 | 是 | 4 - 6 | i386: Linux, Win32 | |
| 16 | | - | | [**Firebird**](http://www.firebirdsql.org/) | [[Firebird|SQLdb]] | 是 | 取决于<sup>1</sup> | 1 - 2.5 | i386: Linux, Win32 | |
| 17 | | - | | **(Visual) FoxPro** | [[Lazarus Tdbf Tutorial|DBFLaz]] | 否 | 否 | 2.0, 2.5, 3.0 (不完全) | 所有 | |
| 18 | | - | | [**Microsoft SQL Server**](http://www.microsoft.com/zh-cn/server-cloud/products/sql-server/) | SQLdb | 是 | 是 | 6- | FPC 2.6.2+. Linux, macOS, Win32, probably *BSD, probably Solaris<sup>2</sup> | |
| 19 | | - | | [**MySQL**](http://www.mysql.com/) | [[mysql|SQLdb]] | 是 | 是 | 3.0 - 5.5 | i386: Linux, Win32 | |
| 20 | | - | | **ODBC** | [[ODBCConn|SQLdb]] | 是 | 取决于 | 3.x <sup>3</sup> | i386: Linux, Win32 | |
| 21 | | - | | [**Oracle**](http://www.oracle.com/cn/) | [[Oracle|SQLdb]] | 是 | 是 | \- | \- | |
| 22 | | - | | **Paradox** | [[TParadoxDataSet]] | 否 | 否 | up to Table Level 7 (and up ??) | 所有 | |
| 23 | | - | | [**PostgreSQL**](http://www.postgresql.org/) | [[postgresql|SQLdb]] | 是 | 是 | 6.6 - 8 | i386: Linux, Win32 | |
| 24 | | - | | [**Sybase Adaptive Server Enterprise (ASE)**](http://www.sybase.com.cn/products/databasemanagement/adaptiveserverenterprise) | [[mssqlconn|SQLdb]] | 是 | 是 | 任何 | Linux, macOS, Win32, probably *BSD, probably Solaris<sup>2)</sup> | |
| 25 | | - | | [**SQLite**](http://www.sqlite.org/) | [[sqlite|SQLdb]] | 是 | 否 | sqlite3 | i386: Linux, Win32 | |
| 26 | | - | | [**SQLite**](http://www.sqlite.org/) | [[sqlite|TSQLite3Dataset]] | 是 | 否 | sqlite2,sqlite3 | i386: Linux, Win32 | |
| 27 | | - | | **Text 文件** | sdf | 否 | 否 | \- | 所有 | |
| 9 | + | ### 支持的数据库 |
| 28 | 10 | | |
| 29 | | - | [[Databases/zh CN|备注 (1):]] 你可以使用 Firebird 对Windows和Linux的嵌入式版本(可能在Mac OS X也行),也可以连接到基于Windows/Unix/macOS/FreeBSD的/其他运行 Firebird 服务器支持的平台 |
| 11 | + | 数据库 | 软件包名 | 是否需要客户端库 | 是否需要服务器端 | 支持的版本 | 支持的平台 |
| 12 | + | ---|---|---|---|---|--- |
| 13 | + | [**Advantage**](<http://www.advantagedatabase.com/>) | TAdsDataSet | 是 | 否 | 10.1 以上 | i386: Linux、Win32 |
| 14 | + | **DBase** | [[Lazarus Tdbf Tutorial/zh CN|DBFLaz]] | 否 | 否 | III+, IV, VII | 所有 |
| 15 | + | **TurboPower FlashFiler** | [[FlashFiler]] | 否 | 否 | - | Win 32、(win64?) |
| 16 | + | **In memory** | [[How to write in-memory database applications in Lazarus/FPC|memds]] | 否 | 否 | - | 所有 |
| 17 | + | **In memory** | [[How to write in-memory database applications in Lazarus/FPC|bufdataset]] | 否 | 否 | - | 所有 |
| 18 | + | [**Firebird**](<http://www.firebirdsql.org/>) | [[Firebird|SQLdb]] | 是 | 依赖于1 | 1 - 2.5 | i386: Linux、Win32 |
| 19 | + | **(Visual) FoxPro** | [[Lazarus Tdbf Tutorial|DBFLaz]] | 否 | 否 | 2.0, 2.5, 3.0 (不完全) | 所有 |
| 20 | + | [**Interbase**](<http://www.embarcadero.com/products/interbase-smp>) | [[Firebird|SQLdb]] | 是 | 是 | 4 - 6 | i386: Linux、Win32 |
| 21 | + | [**Microsoft SQL Server**](<http://www.microsoft.com/sqlserver/>) | SQLdb | 是 | 是 | 6- | FPC 2.6.2+. Linux、macOS、Win32、可能 *BSD、可能 Solaris2 |
| 22 | + | [**MySQL**](<http://www.mysql.com/>) | [[mysql|SQLdb]] | 是 | 是 | 3.0 - 5.5 | i386: Linux、Win32 |
| 23 | + | **ODBC** | [[ODBCConn|SQLdb]] | 是 | 有依赖条件 | 3.x 3 | i386: Linux、Win32 |
| 24 | + | [**Oracle**](<http://www.oracle.com/cn/>) | [[Oracle|SQLdb]] | 是 | 是 | - | \- |
| 25 | + | **Paradox** | [[TParadoxDataSet]] | 否 | 否 | 最高至7级表(以上?) | 所有 |
| 26 | + | **Paradox** | [[TParadox]] | 是 | 否 | | Win32 |
| 27 | + | [**PostgreSQL**](<http://www.postgresql.org/>) | [[postgresql|SQLdb]] | 是 | 是 | 6.6 - 8 | i386: Linux、Win32 |
| 28 | + | [**Sybase Adaptive Server Enterprise (ASE)**](<http://www.sybase.com.cn/products/databasemanagement/adaptiveserverenterprise>) | [[mssqlconn|SQLdb]] | 是 | 是 | 任何 | Linux、macOS、Win32、可能 *BSD, 可能Solaris2) |
| 29 | + | [**SQLite**](<http://www.sqlite.org/>) | [[sqlite|SQLdb]] | 是 | 否 | sqlite3 | 所有 |
| 30 | + | [**SQLite**](<http://www.sqlite.org/>) | [[sqlite|SQLite(3)Laz]] | 是 | 否 | sqlite2、sqlite3 | 所有 |
| 31 | + | **Text 文件** | sdf | 否 | 否 | - | 所有 |
| 32 | + | **ZMSQL** | zmsql | 否 | 否 | ? | ? |
| 33 | + | |
| 34 | + | [[Databases/zh CN|注1:]] 可在 Windows 和 Linux(可能也包括 macOS)中使用嵌入版的 Firebird,或者连到运行于 Windows/Unix/macOS/FreeBSD/其他 Firebird 支持平台中的 Firebird 服务器。 |
| 30 | 35 | | |
| 31 | | - | [[Databases/zh CN|备注 (2):]] 这些连接器使用FreeTDS 的库作为驱动程序,FreeTDS 文档表明,应该建立这些平台。Windows x86/x64可以在这里下载:[1](ftp://ftp.freepascal.org/fpc/contrib/windows/dblib_x32.zip) 和 [2](ftp://ftp.freepascal.org/fpc/contrib/windows/dblib_x64.zip) |
| 36 | + | [[Databases/zh CN|注2:]] 这些连接库用 FreeTDS 作为驱动程序,FreeTDS 的文档指出,至少应该可在这些平台下编译。Windows x86/x64 版本可在此下载:[[1]](<ftp://ftp.freepascal.org/fpc/contrib/windows/dblib_x32.zip>) 和 [[2]](<ftp://ftp.freepascal.org/fpc/contrib/windows/dblib_x64.zip>) |
| 32 | 37 | | |
| 33 | | - | [[Databases/zh CN|备注 (3):]] 此版本号指的是ODBC标准,而不是一个驱动程序或驱动程序管理器的版本号。ODBC 3.x驱动对于大多数DBMS。 |
| 38 | + | [[Databases/zh CN|注3:]] 这里是指 ODBC 标准的版本,而非驱动程序或驱动程序管理器的版本号。大多数 DBMS 都带有 ODBC 3.x 驱动程序。 |
| 34 | 39 | | |
| 35 | | - | ## 数据库客户端绑定 |
| 40 | + | ## 绑定数据库客户端 |
| 36 | 41 | | |
| 37 | | - | If you want to use one of the databases that need client libraries, those libraries have to be installed. Not only on the computer you're programming on, but also on the computers where the application must run. Note that some databases (in particular MySQL) only work if the bindings which are compiled in the application are from the same version as those of the installed libraries. You can find out how to install those libraries (.so files on *nix systems, and .dlls on windows) on the website of the database developers. The binding units can be found in the packages/base directory in the fpc-sources. They basically consist of the client API calls like mysql_connect_database, which are completely different for each database. It is possible to write database applications using these units, but it is usually far more work and bug-sensitive than using the DB-unit Lazarus components. |
| 42 | + | 如果连接数据库需要客户端库,那就必须安装一下。不仅要在编程的计算机上安装,还要在所有运行应用程序的计算机上安装。请注意,某些数据库(特别是 MySQL)要求,编译时绑定的库版本必须与安装运行时的相同。在数据库开发方网站上,可以找到客户端库的安装方法(在 *nix 系统上是 .so 文件,在 Windows 上是 .dll 文件)。有关绑定的代码文件位于 fpc-sources 的 packages/base 目录中。内容基本就是一些客户端 API 的调用,比如 mysql_connect_database,每种数据库的调用各不相同。当然可以用这些代码文件编写数据库应用,但通常会增加很多工作量,且更容易出错,相比之下,使用 Lazarus 的 DB 组件会更为方便。 |
| 38 | 43 | | |
| 39 | | - | Most of these bindings packages are hard-linked to the client libraries. This means that if the application is compiled with one of these units in it, the whole application can not be linked if the client libraries are not available on the workstation. This means that your program executable will not be generated if you do not have installed - for example - a MySQL client on your computer, and you are using the mysql4.pp unit in your program. If you succeed in compiling the program on a computer which has the MySQL client libraries installed, it still won't start on any other machine without the appropriate MySQL client libraries. In other words: for these databases, you need to install client libraries on your development machine, and you need to install these client libraries with your application. |
| 44 | + | 这些绑定软件包大多用硬编码链接客户端库。这意味着,如果在编译时包含了某个单元,但没有合适的客户端库,那么整个应用程序将无法链接。也就是说,如果计算机中没有安装 MySQL 客户端,程序中又使用了 mysql4.pp 单元,那么将无法生成可执行文件。虽然在安装了 MySQL 客户端库的计算机上编译成功,但在没有对应 MySQL 客户端库的其他机器上该程序仍然无法启动。换句话说:这些数据库要在开发机器上安装客户端库,并且还要将这些客户端库与应用程序一起安装。 |
| 40 | 45 | | |
| 41 | | - | To avoid such problems some of the packages are also able to link dynamically to the libraries. Before any calls to those libraries can be made, the unit has to be 'initialized'. This initialization fails if the database client isn't installed on the computer. If the program is ready using the client library, the unit has to be 'released'. |
| 46 | + | 为了避免这类问题,有些包能够动态链接到客户端库。在调用客户端库之前,必须先进行“初始化”。如果计算机上没有安装数据库客户端,初始化就会失败。如果客户端库连接就绪,那么之后需要执行“释放”操作。 |
| 42 | 47 | | |
| 43 | 48 | | ## 数据集 |
| 44 | 49 | | |
| 45 | | - | Database use in Lazarus (or Free Pascal) is fundamentally based on the TDataset class. This represents a table or query to your application. However, like many other such fundamental classes, you don't use the TDataset class itself, you use a descendant of it. There are many of these. They provide access to different kinds of databases, such as local dbase or text files, or back-end databases such as PostgreSQL, Firebird, MySQL and so forth. Some dataset descendants link directly to database tables, while others use additional components or libraries to perform the link. |
| 46 | | - | |
| 47 | | - | Dataset descendants, being non-visual components are (usually) part of the Free Component Library (FCL) rather than the Lazarus Component Library (LCL). |
| 48 | | - | |
| 49 | | - | Datasets can be used both programmatically and with visual controls. A typical Lazarus database application will often use both methods. In either case, the first step is to create the TDataset descendant, initialise it to connect to the table or query you want, and open it. This can be done either in code at run time or by putting a component on your form and setting it's properties at design time. The details of this vary considerably with different TDataset descendants, so see the various guides under [[Databases]] for what has to be done for your database. |
| 50 | | - | |
| 51 | | - | When the dataset is opened, a set of field components are created, one for each field or column of the table or query you opened. Each field component is a descendant of TField, appropriate to the particular data type of the field, eg, TStringField. |
| 50 | + | ### 数据集和数据库的区别 |
| 52 | 51 | | |
| 53 | | - | ### 使用数据集代码 |
| 52 | + | 许多数据库编程新人无法区分数据集和数据库。现在数据库有很多种,比如 Firebird、Interbase、Oracle、Sybase、Microsoft Access 等。 |
| 54 | 53 | | |
| 55 | | - | Programmatic access will be explained in more detail in [[Using Dataset and Field components]], but as a very simple overview: |
| 54 | + | 最简单的概念就是,Lazarus 数据集(TDataset 的子类,如 TSQLQuery 等)可视为部分数据库查询记录的副本。假设用以下 SQL 语句打开查询: |
| 55 | + | ``` |
| 56 | + | SQLQuery1.SQL.Text:= 'select * from table1 where id <=100'; |
| 57 | + | ``` |
| 56 | 58 | | |
| 57 | | - | - Use the TDataset descendant to open the table or query, filter the rows you want to see, and to move from row to row. |
| 58 | | - | - Use the TField descendants to: |
| 59 | | - | - Access general information about fields |
| 60 | | - | - Access the specific data values for the current row. (use the As... properties, such as AsString, AsInteger, etc.) |
| 61 | | - | - Access the fields of a TDataset descendant by using either: |
| 62 | | - | - The fields property, eg Fields\[0\] is the first field, |
| 63 | | - | - The FieldByName method, eg FieldByName('AGE') returns the field associated with the database field called 'AGE' |
| 59 | + | 打开 SQLQuery1,数据库 table1 中 id 小于等于 100 的记录将复制到 SQLQuery1 中。 |
| 64 | 60 | | |
| 65 | | - | See [[Database field type]] for a list of field types. |
| 61 | + | 通过 TDatasource(其又与 TDataset 连接),数据集中的数据可用数据库控件(如 TDBGrid、TDBEdit 等)进行可视化展示。 |
| 66 | 62 | | |
| 67 | | - | ### 使用可视化(数据感知)控制 |
| 63 | + | 数据集中的数据可通过 insert、edit、delete、post 等命令(后续会讲解)进行修改,但这些操作都是基于数据集进行的,而不是直接在数据库表上完成。需要多做一步才能让改动持久性地存入数据库,比如调用 TDataSet.ApplyUpdates 或执行插入、更新 SQL 语句(与 TDataset.insert 或 TDataset.update 不同)。 |
| 68 | 64 | | |
| 69 | | - | 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: |
| 65 | + | 通过设置数据库的事务属性,可在数据集关闭时将改动自动保存(或丢弃)。 |
| 70 | 66 | | |
| 71 | | - | - 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) |
| 72 | | - | - Add a TDatasource component (from the "Data Access" tab) to the form, and "link" it to the dataset (set the DataSet property) |
| 73 | | - | - Add data-aware controls from the "Data Controls" tab to the form, and link each one to the DataSource (not dataset) component |
| 74 | | - | - Most controls link to a single field, so you also need to set the Field for each tab. |
| 67 | + | ### 数据集简介 |
| 75 | 68 | | |
| 76 | | - | See #Data Controls below for more details on the controls |
| 69 | + | 在 Lazarus(或 Free Pascal)中,数据库的使用底层都基于 [[TDataSet]] 类展开。在应用程序中,TDataSet 类代表一张表或一个查询。然而,与很多其他基础类一样,TDataset 类不会直接用到,而会采用其某个子类。这些子类有很多,提供了对各种数据库的访问,例如本地 dBase 或文本文件,或者远程数据库如 PostgreSQL、Firebird、MySQL等。有些子类直接链接到数据库表,而另一些则使用其他组件或库来执行链接。 |
| 77 | 70 | | |
| 78 | | - | ### 数据集状态 |
| 71 | + | Dataset 的子类是非可视化组件,通常属于自由组件库(FCL)而不是 Lazarus 组件库(LCL)。 |
| 79 | 72 | | |
| 80 | | - | 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 |
| 73 | + | 数据集既可通过纯代码方式使用,也可与可视化控件一起使用。典型的 Lazarus 数据库应用通常会同时使用这两种方式。不论哪种方式,第一步都是创建 TDataset 子类,进行初始化以连接到所需的表或查询,并执行 open 操作。可在运行时用代码完成,也可通过在窗体上放置组件并在设计时设置属性来完成。不同的 TDataset 子类在这方面的细节差异很大,因此请参阅[[Databases|数据库]]中的各个手册,了解操作步骤。 |
| 81 | 74 | | |
| 82 | | - | State |
| 75 | + | 数据集打开后,会创建一些字段组件,每个字段(或打开的表、查询的列)都对应一个字段组件。每个字段组件都是 TField 的子类,根据字段的数据类型(如 TStringField)对应某个子类。 |
| 83 | 76 | | |
| 84 | | - | Function |
| 77 | + | ### 通过代码使用数据集对象 |
| 85 | 78 | | |
| 86 | | - | dsInactive |
| 79 | + | 关于以编程方式访问数据集对象的详细说明,请参阅[[Using Dataset and Field components|数据集和字段组件的用法]],不过以下给出了非常简单的描述: |
| 87 | 80 | | |
| 88 | | - | The dataset is closed |
| 81 | + | * 用 TDataset 的子类打开表或查询,过滤出需查看的行,并在各行之间移动。 |
| 82 | + | * 用 TField 的子类: |
| 83 | + | * 访问字段的定义信息 |
| 84 | + | * 访问当前行的字段值(通过 AsString、AsInteger 之类的属性) |
| 85 | + | * 用以下方式访问 TDataset 子类的多个字段: |
| 86 | + | * Fields 属性,如 Fields[0] 是第一个字段 |
| 87 | + | * FieldByName 方法,如 FieldByName('AGE') 返回数据库字段名为“AGE”的字段对象 |
| 89 | 88 | | |
| 90 | | - | dsBrowse |
| 89 | + | 字段类型的清单参见 [[Database field type]]。 |
| 91 | 90 | | |
| 92 | | - | The user can browse through the dataset, looking at values |
| 91 | + | ### 可视化(数据感知)控件的用法 |
| 93 | 92 | | |
| 94 | | - | dsEdit |
| 93 | + | 若要在简单的“RAD”风格的 Lazarus 应用中使用数据库,通常会在设计时配置数据集子类,然后使用数据感知控件。这需要: |
| 95 | 94 | | |
| 96 | | - | The user can edit values on the current row. Values are not saved until a post is performed. |
| 95 | + | * 在窗体中加入所选数据库的数据集子类及支撑组件,并打开(将“Active”属性设为true)。 |
| 96 | + | * 在窗体中加入一个[[TDataSource]]组件(位于[[Data Access tab|Data Access 页]]),并“链接”至数据集组件(设置 DataSet属性)。 |
| 97 | + | * 在窗体中加入数据感知控件(位于[[Data Controls tab|Data Controls 页]],全都要链接至 DataSource 组件(不是数据集组件)。 |
| 98 | + | * 大多数数据感知控件都是要链接到某个字段的,因此还需设置其 Field 属性。 |
| 97 | 99 | | |
| 98 | | - | dsInsert |
| 100 | + | 更多有关控件的信息,请参阅下文中的 #Data Controls。 |
| 99 | 101 | | |
| 100 | | - | A new row has been added, and the user can set the values. The record is not saved until a post is performed |
| 102 | + | ### 数据集的状态 |
| 101 | 103 | | |
| 102 | | - | 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. |
| 104 | + | 数据集可能处于多个状态。状态虽有很多(可以在源代码中查找 TDataSetState),但初学者需要了解的状态主要有: |
| 103 | 105 | | |
| 104 | | - | ### 数据集 UpdateStatus |
| 106 | + | 状态 | 功能 |
| 107 | + | ---|--- |
| 108 | + | dsInactive | 已关闭 |
| 109 | + | dsBrowse | 用户可浏览,查看数据值 |
| 110 | + | dsEdit | 用户可编辑当前行的数据值。在执行 post 前不会保存。 |
| 111 | + | dsInsert | 加入新行,用户可设置值。在执行 post 前此行不会保存。 |
| 112 | + | |
| 113 | + | 其他状态相对短暂,并且通常会“自动”处理,用于内部和更复杂的代码。如果只是查看数据库中的数据,并且在设计时打开数据集,那么很大程度上可以忽略状态的概念,因为大部分时间都会处于 dsBrowse 状态。不过大多数应用都需要修改数据。如果采用数据感知控件,则很多操作都会自动处理。例如[[TDBEdit]]控件在修改文本时,会将数据集置于 dsEdit 状态——除非原来就已处于 dsEdit 或 dsInsert 状态。如果在 dsEdit 或 dsInsert 状态时“滚动”到其他记录去,那么当前编辑的记录将被“提交”,数据集将回到 dsBrowse 状态。但如果是用代码访问数据集,那通常就必须用代码修改状态了。[[TDBNavigator]]控件(见下文)允许用户显式修改状态。 |
| 105 | 114 | | |
| 106 | | - | UpdateStatus determines the current state of the record buffer, if updates have not yet been applied to the database. |
| 115 | + | ### 数据集的 UpdateStatus 属性 |
| 107 | 116 | | |
| 108 | | - | **Example** how to detect if ApplyUpdates will Insert, Update or Delete data: |
| 117 | + | 如果更新操作尚未写入数据库,UpdateStatus 标识了数据记录缓冲区的当前状态。 |
| 109 | 118 | | |
| 110 | | - | ``` pascal |
| 119 | + | **示例** 如何检测 ApplyUpdates 要执行插入、更新还是删除操作: |
| 120 | + | ``` |
| 111 | 121 | | procedure QueryAfterPost(DataSet: TDataSet); |
| 112 | | - | begin |
| 113 | | - | case DataSet.UpdateStatus of |
| 114 | | - | usUnmodified : ShowMessage('Unmodified'); |
| 115 | | - | usModified : ShowMessage('Modified'); |
| 116 | | - | usInserted : ShowMessage('Inserted'); |
| 117 | | - | usDeleted : ShowMessage('Deleted'); |
| 118 | | - | end; |
| 119 | | - | end; |
| 122 | + | begin |
| 123 | + | case DataSet.UpdateStatus of |
| 124 | + | usUnmodified : ShowMessage('Unmodified'); |
| 125 | + | usModified : ShowMessage('Modified'); |
| 126 | + | usInserted : ShowMessage('Inserted'); |
| 127 | + | usDeleted : ShowMessage('Deleted'); |
| 128 | + | end; |
| 129 | + | end; |
| 120 | 130 | | ``` |
| 121 | 131 | | |
| 122 | | - | **Value Explanation:** |
| 132 | + | **解释** |
| 123 | 133 | | |
| 124 | | - | - usUnmodified: Record is unmodified |
| 125 | | - | - usModified: Record exists in the database but is locally modified |
| 126 | | - | - usInserted: Record does not yet exist in the database, but is locally inserted |
| 127 | | - | - usDeleted: Record exists in the database, but is locally deleted |
| 134 | + | * usUnmodified: 记录未修改 |
| 135 | + | * usModified: 数据库中存在记录,本地已修改 |
| 136 | + | * usInserted: 数据库中不存在记录,本地已插入 |
| 137 | + | * usDeleted: 数据库中存在记录,本地已删除 |
| 128 | 138 | | |
| 129 | | - | ### 提交和取消 |
| 139 | + | ### 提交和撤销 |
| 130 | 140 | | |
| 131 | | - | If you have edited or inserted a record, the new values are held in a buffer. |
| 141 | + | 如果记录已完成编辑和插入,新值将保存在缓冲区中。 |
| 132 | 142 | | |
| 133 | | - | - Calling the dataset cancel method removes the new record (insert) or reverts the values to their previous values (edit). |
| 134 | | - | - 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. |
| 143 | + | * 调用数据集对象的 cancel 方法,将会删除新记录(插入操作时)或恢复原值(编辑操作时)。 |
| 144 | + | * 调用数据集对象的 post 方法,将会保存记录值(编辑操作时)或新记录(插入操作时)。 有些数据集子类会立即写入数据库,而另一些则会先存入更新列表中,后续再调用某个操作一次性写入数据库。最后,即便数据已经写入数据库,可能仍需调用“commit”方法让数据库执行持久性保存。所有这些操作,都因数据集子类的不同而有很大差异,因此请查看详细的文档。 |
| 135 | 145 | | |
| 136 | 146 | | ### 插入新记录 |
| 137 | 147 | | |
| 138 | | - | To insert a new record into a TDataset descendent, one should use the method [Insert](http://www.freepascal.org/docs-html/fcl/db/tdataset.insert.html). After that one can set the field values and then finally call Post to commit the new record, as the example below shows. |
| 139 | | - | |
| 140 | | - | The example also shows how to insert BLOB data from a file - you can also use LoadFromStream to load the data from a stream. |
| 148 | + | 要通过 [[TDataSet]] 子类插入新记录,应该使用 [Insert](<http://www.freepascal.org/docs-html/fcl/db/tdataset.insert.html>) 方法。先要设置字段值,最后再调用 Post 提交新记录,如下所示。 |
| 141 | 149 | | |
| 142 | | - | ``` pascal |
| 143 | | - | MyDataset.Insert; |
| 144 | | - | MyDataset.Fields[0].AsInteger := 4; //an integer field |
| 145 | | - | MyDataset.Fields[1].AsString := 'First Name'; //a string field |
| 146 | | - | TBlobField(MyDataset.Fields[2]).LoadFromFile('SomeBlobfile.bin'); //blob field |
| 147 | | - | MyDataset.Post; |
| 150 | + | 以下例子还展示了如何将文件数据插入 BLOB,也可以用 LoadFromStream 从流中加载数据。 |
| 148 | 151 | | ``` |
| 149 | | - | |
| 150 | | - | ### 如何快速跳转到特定记录 |
| 152 | + | MyDataset.Insert; |
| 153 | + | MyDataset.Fields[0].AsInteger := 4; //整数字段 |
| 154 | + | MyDataset.Fields[1].AsString := 'First Name'; //字符串字段 |
| 155 | + | (MyDataset.Fields[2] as TBlobField).LoadFromFile('SomeBlobfile.bin'); //blob 字段 |
| 156 | + | MyDataset.Post; |
| 157 | + | ``` |
| 151 | 158 | | |
| 152 | | - | #### 选择所有记录 |
| 159 | + | ### 快速跳转至表中某条记录 |
| 153 | 160 | | |
| 154 | | - | 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. |
| 161 | + | #### 已查出所有记录 |
| 155 | 162 | | |
| 156 | | - | #### 选择所需记录 |
| 163 | + | 如果是用 SELECT * FROM 查出了表中所有记录,然后要在记录之间快速跳转,那么就得先建索引再检索。更高效的做法是只查询需要的记录。 |
| 157 | 164 | | |
| 158 | | - | One fast solution to jump to a particular record is to select only it, for example doing: |
| 165 | + | #### 只查询所需记录 |
| 159 | 166 | | |
| 160 | | - | ``` pascal |
| 167 | + | 跳转至某条记录的一种做法,就是只查询这条记录,例如: |
| 168 | + | ``` |
| 161 | 169 | | var |
| 162 | | - | MyDataset: TSQLQuery; |
| 163 | | - | begin |
| 164 | | - | //... |
| 165 | | - | MyDataset.FieldDefs.Add('SessionId', ftLargeint); |
| 166 | | - | MyDataset.FieldDefs.Add('GameEvent', ftLargeint); |
| 167 | | - | MyDataset.FieldDefs.Add('TableId', ftInteger); |
| 168 | | - | MyDataset.FieldDefs.Add('LoggedIn', ftBoolean); |
| 169 | | - | MyDataset.FieldDefs.Add('PlayerId', ftInteger); |
| 170 | | - | MyDataset.Active := False; |
| 171 | | - | { Non-parameterized format; may run into issues with text containing ' and dates |
| 172 | | - | SQLText := Format('select * from "GameSession" WHERE "SessionId"=%d', [ASessionId]); |
| 173 | | - | } |
| 174 | | - | // Solution: parameterized query: |
| 175 | | - | // Actually, if this is done in a loop, you only need to set the SQL.Text once, |
| 176 | | - | // and only change the parameter value |
| 177 | | - | MyDataset.SQL.Text := 'select * from "GameSession" WHERE "SessionID"=:SessionID'); |
| 178 | | - | MyDataSet.ParamByName('SessionID').AsLargeInt := ASessionID; |
| 179 | | - | try |
| 180 | | - | MyDataset.Active := True; |
| 181 | | - | except |
| 182 | | - | //... |
| 183 | | - | end; |
| 170 | + | MyDataset: TSQLQuery; |
| 171 | + | begin |
| 172 | + | //... |
| 173 | + | MyDataset.FieldDefs.Add('SessionId', ftLargeint); |
| 174 | + | MyDataset.FieldDefs.Add('GameEvent', ftLargeint); |
| 175 | + | MyDataset.FieldDefs.Add('TableId', ftInteger); |
| 176 | + | MyDataset.FieldDefs.Add('LoggedIn', ftBoolean); |
| 177 | + | MyDataset.FieldDefs.Add('PlayerId', ftInteger); |
| 178 | + | MyDataset.Active := False; |
| 179 | + | { Non-parameterized format; may run into issues with text containing ' and dates |
| 180 | + | SQLText := Format('select * from "GameSession" WHERE "SessionId"=%d', [ASessionId]); |
| 181 | + | } |
| 182 | + | // 解决方案:带参数的查询 |
| 183 | + | // 如果是在循环中,其实 SQL.Text 只需设置一次,后续只要修改参数值即可。 |
| 184 | + | MyDataset.SQL.Text := 'select * from "GameSession" WHERE "SessionID"=:SessionID'); |
| 185 | + | MyDataSet.ParamByName('SessionID').AsLargeInt := ASessionID; |
| 186 | + | try |
| 187 | + | MyDataset.Active := True; |
| 188 | + | except |
| 189 | + | //... |
| 190 | + | end; |
| 184 | 191 | | ``` |
| 185 | 192 | | |
| 186 | | - | You can then read information using something like this: |
| 187 | | - | |
| 188 | | - | ``` pascal |
| 189 | | - | lPlayerId := MyDataset.Fields[4].AsInteger; |
| 193 | + | 然后可以读取数据如下: |
| 190 | 194 | | ``` |
| 191 | | - | |
| 192 | | - | ### 过滤 |
| 195 | + | lPlayerId := MyDataset.Fields[4].AsInteger; |
| 196 | + | ``` |
| 193 | 197 | | |
| 194 | | - | You can filter your dataset to restrict the records to a subset you want (e.g. all surnames starting with Smith). |
| 198 | + | ### 数据过滤 |
| 195 | 199 | | |
| 196 | | - | - Using .Filter: |
| 197 | | - | - TDBF, BufDataset and descendants (including TSQLQuery) use the TDBF filtering parser; see [[Lazarus Tdbf Tutorial|Lazarus Tdbf Tutorial#Expressions]] for details on using this. |
| 198 | | - | - TMemDS does not support .Filter |
| 199 | | - | - Using a callback/event procedure with OnFilter where you can program your own filter function |
| 200 | + | 过滤操作可将数据集限制为所需子集(比如所有以 Smith 开头的姓氏)。 |
| 200 | 201 | | |
| 201 | | - | ### 定位/查找 |
| 202 | + | * 使用 .Filter: |
| 203 | + | * [[TDbf]]、[[TBufDataset]] 及其子类(包括 [[TSQLQuery]])使用 TDBF 过滤语法,用法详见 [[Lazarus Tdbf Tutorial|Lazarus Tdbf 使用教程]]。 |
| 204 | + | * [[TMemDataset]] 不支持 .Filter。 |
| 205 | + | * 用 OnFilter [[callback|回调]]/事件处理函数,可编写自己的过滤函数。 |
| 202 | 206 | | |
| 203 | | - | Although more often used in non-SQL datasets (e.g. Paradox, TDBF) you can jump between records using [[locate]]/[[lookup]]. |
| 207 | + | ### locate/lookup 方法 |
| 204 | 208 | | |
| 205 | | - | ### 使用 TSQLQuery |
| 209 | + | [[locate]]/[[lookup]] 可用于在记录之间跳转,但更多是在非SQL数据集中使用(如[[TParadoxDataSet]]、[[TDbf]])。 |
| 206 | 210 | | |
| 207 | | - | For more information about TSQLQuery see [[Working With TSQLQuery]] |
| 211 | + | ### TSQLQuery 用法 |
| 208 | 212 | | |
| 209 | | - | ### 导出 |
| 213 | + | [[TSQLQuery]] 详见 [[Working With TSQLQuery]]。 |
| 210 | 214 | | |
| 211 | | - | FPC/Lazarus contains functionality to let you export datasets to various formats; see e.g. |
| 215 | + | ### 数据导出 |
| 212 | 216 | | |
| 213 | | - | - [[fpXMLXSDExport]] |
| 214 | | - | - [[fpdbfexport]] |
| 215 | | - | - the other components on the Data Export tab |
| 217 | + | FPC/Lazarus 提供了将数据集以多种格式导出的工具包: |
| 216 | 218 | | |
| 217 | | - | Of course, you could also do it manually (see e.g. [[FPSpreadsheet|FPSpreadsheet#Converting_a_database_to_a_spreadsheet]] for export to Excel format using fpspreadsheet) |
| 219 | + | * [[fpXMLXSDExport]] |
| 220 | + | * [[fpdbfexport]] |
| 221 | + | * Data Export 页上的其他组件 |
| 218 | 222 | | |
| 219 | | - | ## 数据控制 |
| 223 | + | 当然,也可以手动导出(比如用 fpspreadsheet 导出为 Excel 格式的 [[FPSpreadsheet|FPSpreadsheet#Converting_a_database_to_a_spreadsheet]])。 |
| 220 | 224 | | |
| 221 | | - | 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. |
| 225 | + | ## Data Controls 控件组 |
| 222 | 226 | | |
| 223 | | - | ### 数据源控制 |
| 227 | + | 使用这些控件需先加到窗体中,并至少应设置 datasource 属性。下面会介绍其他的关键属性。 |
| 224 | 228 | | |
| 225 | | - | 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). |
| 229 | + | ### Datasource 控件 |
| 226 | 230 | | |
| 227 | | - | ### 单字段控制 |
| 231 | + | 此控件用于跟踪所关联控件当前位于哪条记录。Datasource 控件必须关联某个数据集控件(如 TSQLQuery)。 |
| 228 | 232 | | |
| 229 | | - | These controls all attach to a single field. As well as datasource, set the field name. Controls include: |
| 233 | + | ### 单字段控件 |
| 230 | 234 | | |
| 231 | | - | - [[doc:lcl/dbctrls/tdbtext.html|DBText control]] Displays a text field (readonly, no border) |
| 232 | | - | - [[doc:lcl/dbctrls/tdbedit.html|DBEdit control]] Displays / edits a text field as an edit box |
| 233 | | - | - [[doc:lcl/dbctrls/tdbmemo.html|DBMemo control]] Displays / edits a text field in a multi-line edit box |
| 234 | | - | - [[doc:lcl/dbctrls/tdbimage.html|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|Lazarus For Delphi Users#TDBImage]] |
| 235 | | - | - [[doc:lcl/dbctrls/tdblistbox.html|DBListBox control]] and [[doc:lcl/dbctrls/tdbcombobox.html|DBComboBox Control]] Allow the user to insert values into a database field from the list of values in the Items property of the controls |
| 236 | | - | - [[doc:lcl/dbctrls/tdblookuplistbox.html|DBLookupListBox control]] and [[doc:lcl/dbctrls/tdblookupcombobox.html|DBLookupComboBox control]], see also [[dblookupcombobox]] 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. |
| 237 | | - | - [[doc:lcl/dbctrls/tdbcheckbox.html|DBCheckBox control]] Displays / edits a boolean field by checking/clearing a check box |
| 238 | | - | - [[doc:lcl/dbctrls/tdbradiogroup.html|DBRadioGroup control]] Displays the items as in a normal radio group, reading/setting the field value from a matching values list |
| 239 | | - | - [[doc:lcl/dbctrls/tdbcalendar.html|DBCalendar control]] Displays / edits a date field using a calendar panel |
| 240 | | - | - [[doc:lcl/dbctrls/tdbgroupbox.html|DBGroupBox control]] |
| 235 | + | 这些控件都是用于关联单个字段。除了设置 Datasource 外,还需设置字段名称。包括了: |
| 241 | 236 | | |
| 242 | | - | ### [[doc:lcl/dbgrids/tdbgrid.html|DBGrid control]] |
| 237 | + | * [DBText 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbtext.html> "doc:lcl/dbctrls/tdbtext.html") 显示文本字段(只读,无边框)。 |
| 238 | + | * [DBEdit 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbedit.html> "doc:lcl/dbctrls/tdbedit.html") 以文本框的形式显示/编辑文本字段。 |
| 239 | + | * [DBMemo 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbmemo.html> "doc:lcl/dbctrls/tdbmemo.html") 以多行文本框的形式显示/编辑文本字段。 |
| 240 | + | * [DBImage 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbimage.html> "doc:lcl/dbctrls/tdbimage.html") 显示以 BLOB 格式存于数据库中的图片。注意:Lazarus 在将图像数据存入数据库的 BLOB 字段时,默认会写入包含图像类型的头部信息。这与 Delphi 不同。但可以让 TDBImage 兼容 Delphi,参见[[Lazarus For Delphi Users|Delphi 用户的 Lazarus 指南]]。 |
| 241 | + | * [DBListBox 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdblistbox.html> "doc:lcl/dbctrls/tdblistbox.html") 和 [DBComboBox 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbcombobox.html> "doc:lcl/dbctrls/tdbcombobox.html") 允许用户从其 Items 属性值列表中选取数据并插入数据库字段。 |
| 242 | + | * [DBLookupListBox 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdblookuplistbox.html> "doc:lcl/dbctrls/tdblookuplistbox.html") 和 [DBLookupComboBox 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdblookupcombobox.html> "doc:lcl/dbctrls/tdblookupcombobox.html"),参见 [[TDBLookupComboBox]] 通过显示由另一张表字段查出的数据,允许用户从中选取值插入数据库字段中。尽管这些控件将结果存于单个字段中,但需要用到其他字段存放要查找的数据。**注:** 至少 FPC 2.6.0 的 DBLookupComboBox 存在一个 bug,要求 listfield 也必须同在 Datasource 中。只要在 Datasource 的数据集中声明一个与 listfield 同名但不执行操作的计算字段,即可绕过这个问题。 |
| 243 | + | * [DBCheckBox 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbcheckbox.html> "doc:lcl/dbctrls/tdbcheckbox.html") 以选中/清除勾选框的形式显示/编辑 boolean 字段。 |
| 244 | + | * [DBRadioGroup 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbradiogroup.html> "doc:lcl/dbctrls/tdbradiogroup.html") 以单选按钮组的形式显示多项数据,由匹配上的某项数据读取/设置字段值。 |
| 245 | + | * [DBCalendar 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbcalendar.html> "doc:lcl/dbctrls/tdbcalendar.html") 以日历面板的形式显示/编辑日期字段。 |
| 246 | + | * [DBGroupBox 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbgroupbox.html> "doc:lcl/dbctrls/tdbgroupbox.html") |
| 243 | 247 | | |
| 244 | | - | 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 [[doc:lcl/dbgrids/tdbgrid.columns.html|columns collection]] to restrict it to specific fields and to set the widths and titles of each column. |
| 248 | + | ### [DBGrid 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbgrids/tdbgrid.html> "doc:lcl/dbgrids/tdbgrid.html") |
| 245 | 249 | | |
| 246 | | - | Apart from the [[doc:lcl/dbgrids/tdbgrid.html|mentioned documentation]], some more detail can be found here: [[Grids Reference Page|Grids Reference Page#TCustomDBGrid]] |
| 250 | + | 此控件以行/列布局的形式显示多个字段——其实默认显示所有字段。但可在[列集合](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbgrids/tdbgrid.columns.html> "doc:lcl/dbgrids/tdbgrid.columns.html")中添加字段项,以便仅显示指定字段并设置列宽和标题。 |
| 247 | 251 | | |
| 248 | | - | ### [Navigator Control](http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbnavigator.html) |
| 252 | + | 除 [DBGrid 文档](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbgrids/tdbgrid.html> "doc:lcl/dbgrids/tdbgrid.html") 之外,[[Grids Reference Page|Grids Reference Page#TCustomDBGrid]] 也包含更多信息。 |
| 249 | 253 | | |
| 250 | | - | This control gives the user some direct control over the dataset. It allows the user to: |
| 254 | + | ### [TBNavigator 控件](<http://lazarus-ccr.sourceforge.net/docs/lcl/dbctrls/tdbnavigator.html>) |
| 251 | 255 | | |
| 252 | | - | - Move to the next or previous record, or to the start or end of the records |
| 253 | | - | - Add a new record (equivalent to a dataset.insert method call) |
| 254 | | - | - Put the dataset into edit mode |
| 255 | | - | - Delete a record |
| 256 | | - | - Post or Cancel current changes |
| 257 | | - | - Refresh the data (useful in multiuser database applications) |
| 256 | + | 此控件能让用户更直接地控制数据集。可以: |
| 258 | 257 | | |
| 259 | | - | Key Properties: |
| 258 | + | * 移至下一条、上一条、第一条或最后一条记录。 |
| 259 | + | * 加入新记录(等效于调用数据集的 insert 方法)。 |
| 260 | + | * 将数据集设为 edit 模式。 |
| 261 | + | * 删除一条记录。 |
| 262 | + | * 提交或撤销当前改动。 |
| 263 | + | * 刷新数据(多用户数据库应用会比较有用)。 |
| 260 | 264 | | |
| 261 | | - | - 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. |
| 262 | | - | - Width: If you do not show all buttons, you may want to set the width to (height*number_of_visible_buttons) |
| 265 | + | 主要属性: |
| 263 | 266 | | |
| 264 | | - | ## 运行FPC数据库测试 |
| 267 | + | * VisibleButtons:控制用户能够执行的操作。假定不允许删除,就可隐藏删除按钮。如果已有 DBGrid 连了同一个数据集,可能需要控制“下一条”和“上一条”按钮是否启用。 |
| 268 | + | * Width:如果所有按钮都不用显示,可能需要设置控件宽度。 |
| 265 | 269 | | |
| 266 | | - | 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). |
| 270 | + | ## FPC 数据库应用的测试 |
| 267 | 271 | | |
| 268 | | - | To run the test framework on a certain database: |
| 272 | + | Free Pascal 数据库组件包含了一个基于 [[fpcunit]] 的代码测试框架 dbtestframework,可用于验证代码的功能是否正常。 参见 FPC 源码目录 source\packages\fcl-db\tests\。 Included 是个可运行各种数据库组件的测试框架,还包含了一些其他测试(比如数据库输出)。 |
| 269 | 273 | | |
| 270 | | - | 1\. 保存 source\packages\fcl-db\tests\database.ini.txt as source\packages\fcl-db\tests\database.ini |
| 274 | + | 请按以下步骤配置数据库并运行测试框架: |
| 271 | 275 | | |
| 272 | | - | 2\. 修改 source\packages\fcl-db\tests\database.ini to choose which database type you will use. |
| 276 | + | 1\. 将 source\packages\fcl-db\tests\database.ini.txt 保存为 source\packages\fcl-db\tests\database.ini |
| 273 | 277 | | |
| 274 | | - | Example for Interbase/Firebird: |
| 278 | + | 2\. 修改 source\packages\fcl-db\tests\database.ini ,选取所需的数据库类型。 |
| 275 | 279 | | |
| 276 | | - | ``` ini |
| 280 | + | 连接 Interbase/Firebird 数据库的示例: |
| 281 | + | ``` |
| 277 | 282 | | [Database] |
| 278 | | - | type=interbase |
| 283 | + | type=interbase |
| 279 | 284 | | ``` |
| 280 | 285 | | |
| 281 | | - | 3\. In the same file, customize settings for your database. E.g. if you chose interbase before: |
| 282 | | - | |
| 283 | | - | ``` ini |
| 286 | + | 3\. 在同一个文件中配置数据库参数。比如上面选的 Interbase: |
| 287 | + | ``` |
| 284 | 288 | | [interbase] |
| 285 | | - | connector=sql |
| 286 | | - | connectorparams=interbase |
| 287 | | - | ; Database name/path (note: database needs to exist already) |
| 288 | | - | ; You can use aliases (see aliases.conf in your Firebird documentation) |
| 289 | | - | name=testdb |
| 290 | | - | user=sysdba |
| 291 | | - | password=masterkey |
| 292 | | - | ; your hostname may very well differ: |
| 293 | | - | ; Leave blank if you want to use an embedded Firebird database |
| 294 | | - | hostname=192.168.0.42 |
| 289 | + | connector=sql |
| 290 | + | connectorparams=interbase |
| 291 | + | ; Database name/path (note: database needs to exist already) |
| 292 | + | ; You can use aliases (see aliases.conf in your Firebird documentation) |
| 293 | + | name=testdb |
| 294 | + | user=sysdba |
| 295 | + | password=masterkey |
| 296 | + | ; your hostname may very well differ: |
| 297 | + | ; Leave blank if you want to use an embedded Firebird database |
| 298 | + | hostname=192.168.0.42 |
| 295 | 299 | | ``` |
| 296 | 300 | | |
| 297 | | - | 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). |
| 301 | + | 4\. 编译运行 source\packages\fcl-db\tests\dbtestframework.pas (也可用 Lazarus 编译运行 GUI 版的代码 dbtestframework_gui) 若是在 Windows 中使用嵌入式数据库(如 Firebird 嵌入版或 sqlite),请先将所需的 DLL 文件复制到程序所在目录中。输出结果是 XML 格式(dbtestframework_gui 则会显示在屏幕上)。 |
| 298 | 302 | | |
| 299 | | - | Please see source\packages\fcl-db\tests\README.txt for more details. |
| 303 | + | 更多详情请参阅 source\packages\fcl-db\tests\README.txt。 |
| 300 | 304 | | |
| 301 | | - | ## Lazarus包含数据库包 |
| 305 | + | ## Lazarus 提供的数据库软件包 |
| 302 | 306 | | |
| 303 | 307 | | ### sqldblaz.lpk |
| 304 | 308 | | |
| 305 | | - |  |
| 309 | + | [](</File:sqldbcomponents.png>) |
| 306 | 310 | | |
| 307 | | - | This package provides access to different databases. These include: |
| 311 | + | 用于访问多种数据库,包括: |
| 308 | 312 | | |
| 309 | | - | - Interbase/Firebird |
| 310 | | - | - Microsoft SQL Server (except on Lazarus/FPC x64 for Windows) |
| 311 | | - | - MySQL |
| 312 | | - | - Oracle (except on Lazarus/FPC x64 for Windows) |
| 313 | | - | - PostgreSQL (except on Lazarus/FPC x64 for Windows) |
| 314 | | - | - SQLite (with support for the [[Spatialite]] extension) |
| 315 | | - | - Sybase ASE (Adaptive Server Enterprise - not to be confused with Sybase ASA) (except on Lazarus/FPC x64 for Windows) |
| 316 | | - | - any database that has an ODBC driver. |
| 313 | + | * Interbase/Firebird |
| 314 | + | * Microsoft SQL Server(Lazarus/FPC x64 Windows 版除外) |
| 315 | + | * MySQL |
| 316 | + | * Oracle(Lazarus/FPC x64 Windows 版除外) |
| 317 | + | * PostgreSQL(Lazarus/FPC x64 Windows 版除外) |
| 318 | + | * SQLite(支持 [[Spatialite]] 扩展) |
| 319 | + | * Sybase ASE(Adaptive Server Enterprise,请勿与 Sybase ASA 混淆)(Lazarus/FPC x64 Windows 版除外) |
| 320 | + | * 带 ODBC 驱动的所有数据库 |
| 317 | 321 | | |
| 318 | | - | The components (TSQLQuery, TSQLTransaction, TIBConnection, TODBCConnection, TOracleConnection, TMSSQLConnection, TMySQL40Connection, TMySQL41Connection, TMySQL50Connection, TPQConnection, TSybaseConnection) are on the 'SQLdb' tab in the component palette. |
| 322 | + | 所有组件(TSQLQuery、TSQLTransaction、TIBConnection、TODBCConnection、TOracleConnection、TMSSQLConnection、TMySQL40Connection、TMySQL41Connection、TMySQL50Connection、TPQConnection、TSybaseConnection)均位于组件面板的 SQLdb 页。 |
| 319 | 323 | | |
| 320 | | - | - [[SQLdb Package]] |
| 324 | + | * [[SQLdb Package]] |
| 321 | 325 | | |
| 322 | 326 | | ### dbflaz.lpk |
| 323 | 327 | | |
| 324 | | - | 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. |
| 328 | + | 用于访问 dBase 和 FoxPro 数据库。详见 [[Lazarus Tdbf Tutorial|Lazarus Tdbf 教程]]。TDbf 组件位于组件面板的 [[Data Access tab|Data Access 页]]。 |
| 325 | 329 | | |
| 326 | 330 | | ### sqlitelaz.lpk |
| 327 | 331 | | |
| 328 | | - | This package provides access to SQLite databases. You can get more information in the [[Lazarus Database Overview]]. |
| 332 | + | 用于访问 SQLite 数据库。详见 [[Lazarus Database Overview|Lazarus 数据库开发概述]]。 |
| 329 | 333 | | |
| 330 | 334 | | ### sdflaz.lpk |
| 331 | 335 | | |
| 332 | | - | The component TSdfDataSet can be found on the 'Data Access' tab in the component palette. |
| 336 | + | [[TSdfDataSet]] 位于组件面板的 [[Data Access tab|Data Access 页]]。 |
| 333 | 337 | | |
| 334 | 338 | | ### lazreport.lpk |
| 335 | 339 | | |
| 336 | | - | The homepage of the report generator is [<http://lazreport.sourceforge.net/>](http://lazreport.sourceforge.net/). More informationen (et al. an additional link) can be found [[Projects using Lazarus|here]]. LazReport depends on the Printer4Lazarus package. With revision 11950 LazReport was included in the Lazarus SVN repository. |
| 340 | + | 报告生成器的项目主页位于 <http://lazreport.sourceforge.net/>。 更多信息(其他链接)详见[[Projects using Lazarus|这里]]。 LazReport 依赖 Printer4Lazarus 包。 Lazarus SVN 仓库纳入了 LazReport 11950 修订版。 |
| 337 | 341 | | |
| 338 | | - | ## 外部包/库 |
| 342 | + | ### lazdbexport.lpk |
| 339 | 343 | | |
| 340 | | - | ### Zeos 数据库对象 |
| 344 | + | 参见 [[lazdbexport]]。 |
| 341 | 345 | | |
| 342 | | - | These components provide access to different databases. You can find more information [[ZeosDBO|here]]. This wiki also contains a [[Zeos tutorial]]. |
| 346 | + | ## 外部软件包/库 |
| 343 | 347 | | |
| 344 | | - | ### Pascal 数据对象 |
| 348 | + | ### Zeos DataBase Objects |
| 345 | 349 | | |
| 346 | | - | There is now an alternative. |
| 350 | + | 这些组件用于访问各种数据库。更多信息请参阅[[ZeosDBO|此处]],自带 [[Zeos tutorial|Zeos 教程]]。 |
| 347 | 351 | | |
| 348 | | - | 支持: |
| 352 | + | ### Pascal Data Objects |
| 349 | 353 | | |
| 350 | | - | - MySQL 4.1 and 5.0 |
| 351 | | - | - sqlite-2 and sqlite-3 |
| 352 | | - | - pgsql-8.1 |
| 353 | | - | - interbase-5, interbase-6, firebird-1.0, firebird-1.5, firebird-1.5E, firebird-2.0, firebird-2.0E |
| 354 | | - | - mssql (Microsoft library) and sybase (FreeTDS library) |
| 355 | | - | - oracle |
| 354 | + | 已有替代品。 |
| 356 | 355 | | |
| 357 | | - | 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: |
| 356 | + | 支持: |
| 358 | 357 | | |
| 359 | | - | <http://pdo.sourceforge.net> |
| 358 | + | * MySQL 4.1 和 5.0 |
| 359 | + | * sqlite-2 和 sqlite-3 |
| 360 | + | * pgsql-8.1 |
| 361 | + | * interbase-5、interbase-6、firebird-1.0、firebird-1.5、firebird-1.5E、firebird-2.0、firebird-2.0E |
| 362 | + | * mssql(Microsoft 库)和 sybase(FreeTDS 库) |
| 363 | + | * oracle |
| 360 | 364 | | |
| 365 | + | 数据库 API Pascal Data Objects 支持预编译语句、绑定和存储过程等功能,设计灵感来自 PHP Data Objects。所有代码和文档均在 Sourceforge: <http://pdo.sourceforge.net> |
| 366 | + | |
| 361 | 367 | | ### TPSQL |
| 362 | 368 | | |
| 363 | | - | These components provide access via TCP/IP to PostgreSQL databases. You can find more information on [[TPSQL|this page]]. |
| 369 | + | 通过 TCP/IP 访问 PostgreSQL 数据库。详见 [[TPSQL]]。 |
| 364 | 370 | | |
| 365 | 371 | | ### FIBL |
| 366 | 372 | | |
| 367 | | - | These components provide access to Interbase and Firebird databases. The homepage is [<http://sourceforge.net/projects/fibl>](http://sourceforge.net/projects/fibl). |
| 373 | + | 用于访问 Interbase 和 Firebird 数据库。主页为 [[3]](<http://sourceforge.net/projects/fibl>)。 |
| 368 | 374 | | |
| 369 | 375 | | ### IBX |
| 370 | 376 | | |
| 371 | | - | IBX For Lazarus are components to access Firebird databases: see [[IBX]] |
| 377 | + | 用于访问 Firebird 数据库,参见[[IBX]]。 |
| 372 | 378 | | |
| 373 | | - | ### FBLib Firebird 库 |
| 379 | + | ### FBLib Firebird Library |
| 374 | 380 | | |
| 375 | | - | [FBLib](http://fblib.altervista.org/) is an open Source Library No Data Aware for direct access to Firebird Relational Database from Borland Delphi/Kylix, Free Pascal and Lazarus. |
| 381 | + | [FBLib](<http://fblib.altervista.org/>) 是个开源库,无数据感知能力,用于 Borland Delphi/Kylix、Free Pascal 和 Lazarus 直接访问 Firebird 关系数据库。 |
| 376 | 382 | | |
| 377 | | - | Current Features include: |
| 383 | + | 目前支持以下特性: |
| 378 | 384 | | |
| 379 | | - | - Direct Access to Firebird 1.0.x, 1.5.x and 2.x Classic or SuperServer |
| 380 | | - | - Multiplatform \[Win32,Gnu/Linux,FreeBSD) |
| 381 | | - | - Automatic select client library 'fbclient' or 'gds32' |
| 382 | | - | - Query with params |
| 383 | | - | - Support SQL Dialect 1/3 |
| 384 | | - | - LGPL License agreement |
| 385 | | - | - Extract Metadata |
| 386 | | - | - Simple Script Parser |
| 387 | | - | - Only 100-150 KB added into final EXE |
| 388 | | - | - Support BLOB Fields |
| 389 | | - | - Export Data to HTML SQL Script |
| 390 | | - | - Service manager (backup,restore,gfix...) |
| 391 | | - | - Events Alerter |
| 385 | + | * 直接访问 Firebird 1.0.x、1.5.x、2.x Classic 或 SuperServer |
| 386 | + | * 跨平台 [Win32、Gnu/Linux、FreeBSD) |
| 387 | + | * 自动选取 fbclient 或 gds32 客户端库 |
| 388 | + | * 带参数查询语句 |
| 389 | + | * 支持 SQL Dialect 1/3 |
| 390 | + | * LGPL 授权协议 |
| 391 | + | * 读取元数据 |
| 392 | + | * 简单的脚本解析 |
| 393 | + | * 最终 EXE 文件大小只增加 100-150 KB |
| 394 | + | * 支持 BLOB 字段类型 |
| 395 | + | * 数据导出为 HTML SQL 脚本 |
| 396 | + | * 服务管理程序(备份、恢复、gfix 等) |
| 397 | + | * 事件告警 |
| 392 | 398 | | |
| 393 | | - | You can download documentation on [FBLib's website](http://fblib.altervista.org/). |
| 399 | + | 文档见 [FBLib 网站](<http://fblib.altervista.org/>)。 |
| 394 | 400 | | |
| 395 | 401 | | ### Unified Interbase |
| 396 | 402 | | |
| 397 | | - | UIB provides access to Interbase, Firebird and YAFFIL databases. The homepage is [www.progdigy.com](http://www.progdigy.com). A svn repository is available under <https://uib.svn.sourceforge.net/svnroot/uib> . |
| 403 | + | UIB 支持访问 Interbase、Firebird 和 YAFFIL 数据库。主页为 [www.progdigy.com](<http://www.progdigy.com>)。svn 仓库 <https://uib.svn.sourceforge.net/svnroot/uib。> |
| 398 | 404 | | |
| 399 | | - | ### TechInsite Object Persistence 框架 (tiOPF) |
| 405 | + | ### TechInsite Object 持久化框架 (tiOPF) |
| 400 | 406 | | |
| 401 | | - | More information about tiOPF can be found on this [[tiOPF|page]]. |
| 407 | + | 详见 [[tiOPF]]。 |
| 402 | 408 | | |
| 403 | | - | ### Advantage TDataSet Descendant |
| 409 | + | ### 访问 Advantage 的 TDataSet 子类 |
| 404 | 410 | | |
| 405 | | - | 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. |
| 411 | + | Advantage TDataSet 子类可用于连接 Advantage 数据库服务器(访问表)。Advantage 是一种灵活的、无需管理的嵌入式数据库,支持客户端/服务器或点对点方式访问 Clipper、FoxPro 和 Visual FoxPro 9 的 DBF 文件格式,同时还支持一种专有文件格式并提供迁移方案,以便用户能应用新特性。 |
| 406 | 412 | | |
| 407 | | - | Key Features: |
| 413 | + | 主要特性: |
| 408 | 414 | | |
| 409 | | - | - Royalty-free peer-to-peer database access with migration path to Client/Server |
| 410 | | - | - Multi-Platform (Clients supported on Windows and Linux, Server supported on Windows, Linux, and NetWare) |
| 411 | | - | - Supports Both navigational and relational SQL database access |
| 412 | | - | - Full-text search engine |
| 413 | | - | - Table, Index, Memo, and communication encryption |
| 414 | | - | - Compatible with native TDataset components |
| 415 | | - | - Online Backup |
| 416 | | - | - Server supports Replication |
| 415 | + | * 支持免费由点对点方式迁移至客户端/服务器模式。 |
| 416 | + | * 跨平台(客户端支持 Windows 和 Linux,服务器端支持 Windows、Linux 和 NetWare)。 |
| 417 | + | * 同时支持支持导航式访问和关系型 SQL 数据库式访问。 |
| 418 | + | * 全文本搜索引擎。 |
| 419 | + | * 表、索引、Memo 和通讯加密。 |
| 420 | + | * 与原生 TDataset 组件兼容。 |
| 421 | + | * 在线备份 |
| 422 | + | * 支持复制的数据库服务器 |
| 417 | 423 | | |
| 418 | | - | For more information, see the [Advantage Database Server](http://www.advantagedatabase.com) website. |
| 424 | + | 更多信息,请参阅网站 [Advantage Database Server](<http://www.advantagedatabase.com>)。 |
| 419 | 425 | | |
| 420 | | - | ### ZMSQL, sql-enhanced 内存数据库 |
| 426 | + | ### ZMSQL:增强型 SQL 内存数据库 |
| 421 | 427 | | |
| 422 | | - | For more information, see the [ZMSQL wiki page](http://wiki.lazarus.freepascal.org/ZMSQL) |
| 428 | + | 更多信息,请参阅 [[ZMSQL|ZMSQL wiki]]。 |
| 423 | 429 | | |
| 424 | | - | 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. |
| 430 | + | ZMSQL 是开源的、基于 TBufDataset 子类的增强型 SQL 内存数据库,专为 Free Pascal(FPC)设计,基于分号分隔的文本表进行操作。ZMSQL 完全用 Pascal 语言编写,不依赖任何外部库,SQL 功能用 JanSQL 引擎实现。 |
| 425 | 431 | | |
| 426 | | - | 它提供了: |
| 432 | + | 支持以下特性: |
| 427 | 433 | | |
| 428 | | - | - Loading from and saving to flat text tables |
| 429 | | - | - Use of SQL to query the data |
| 430 | | - | - Copy data and schema from other datasets |
| 431 | | - | - Option to predefine fielddefs or create it on-the fly |
| 432 | | - | - Master/detail filtering |
| 433 | | - | - Referential integrity |
| 434 | | - | - Parameterized queries |
| 434 | + | * 文本表的加载/保存 |
| 435 | + | * 用 SQL 查询数据 |
| 436 | + | * 从其他数据集复制数据和模式 |
| 437 | + | * 字段定义可选择预定义或即时创建 |
| 438 | + | * 主/从表过滤 |
| 439 | + | * 参照完整性 |
| 440 | + | * 参数化查询 |
| 435 | 441 | | |
| 436 | | - | The download contains the source code, some demo applications illustrating the features of the component as well as a readme. |
| 442 | + | 可下载的内容包括了源代码、演示应用(用于说明组件的功能)和自述文件。 |
| 437 | 443 | | |
| 438 | | - | ## 参见 |
| 444 | + | ## 参阅 |
| 439 | 445 | | |
| 440 | | - | - [[Portal:Databases|Database Portal]] |
| 441 | | - | - [[Lazarus DB Faq]] |
| 446 | + | * [[Portal:Databases|数据库门户页]] |
| 447 | + | * [[Lazarus DB Faq|Lazarus DB 常见问题]] |
| 442 | 448 | | |
| 443 | 449 | | --- |
| 444 | | - | *From the Free Pascal wiki, page "Databases/zh CN", as it stood on 2020-02-16 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
| 450 | + | *From the Free Pascal wiki, page "Databases/zh CN", as the Internet Archive kept it on 2025-02-17, last changed on 2024-12-30. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |