Difference between revisions of "SQLdb Package"

From Lazarus wiki
Jump to navigationJump to search
(Better/more links)
(Links to documentation)
Line 2: Line 2:
  
 
The SQLdb package contains FPC units to access a number of SQL databases.  It is "packaged" as sqldblaz.lpk in Lazarus, and the components can be found on the "SQLdb" tab.
 
The SQLdb package contains FPC units to access a number of SQL databases.  It is "packaged" as sqldblaz.lpk in Lazarus, and the components can be found on the "SQLdb" tab.
 +
 +
== Documentation ==
 +
See [http://www.freepascal.org/docs-html/fcl/sqldb/index.html SQLDB Documentation]
  
 
== Components ==
 
== Components ==
Line 7: Line 10:
  
 
==== TXXXConnection ====
 
==== TXXXConnection ====
 +
Documentation: see [http://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.html TSQLConnection odocumentation]
 +
 
There are various connection components called TXXXConnection, where XXX is the flavour of the database you are connecting to. Each one of these components takes the requests of the SQLQuery and SQLTransaction components and translates them into requests specifically tailored for the database you are using.
 
There are various connection components called TXXXConnection, where XXX is the flavour of the database you are connecting to. Each one of these components takes the requests of the SQLQuery and SQLTransaction components and translates them into requests specifically tailored for the database you are using.
 
These connection objects descend from the generic TSQLConnection; you can use TSQLConnection to create database programs that can connect to multiple databases (see [[SQLdb Tutorial3]].
 
These connection objects descend from the generic TSQLConnection; you can use TSQLConnection to create database programs that can connect to multiple databases (see [[SQLdb Tutorial3]].
Line 25: Line 30:
  
 
==== TSQLTransaction ====
 
==== TSQLTransaction ====
 +
Documentation: see [http://www.freepascal.org/docs-html/fcl/sqldb/tsqltransaction.html TSQLTransaction documentation]
 +
 
This encapsulates the transaction on the database server. A TXXXConnection object always needs at least one TSQLTransaction associated with it, so that the transaction of its queries is managed.
 
This encapsulates the transaction on the database server. A TXXXConnection object always needs at least one TSQLTransaction associated with it, so that the transaction of its queries is managed.
 
Queries/actions on the database need to be encapsulated in
 
Queries/actions on the database need to be encapsulated in
Line 36: Line 43:
  
 
==== TSQLQuery ====
 
==== TSQLQuery ====
 +
Documentation: see [http://www.freepascal.org/docs-html/fcl/sqldb/tsqlquery.html TSQLQuery ]
 +
 
This is a descendant of TDataset, and provides the data as a table from the SQL query that you submit. However, it can also be used to execute SQL queries (e.g. stored procedures, INSERT INTO..) that don't return any data. See [[Working With TSQLQuery]] for more details.
 
This is a descendant of TDataset, and provides the data as a table from the SQL query that you submit. However, it can also be used to execute SQL queries (e.g. stored procedures, INSERT INTO..) that don't return any data. See [[Working With TSQLQuery]] for more details.
  
Line 54: Line 63:
 
*  Add some controls from the Data Controls tab.  For a quick check, add a TDBGrid component, and set its datasource property to the Datasource component you just added.  You should see the data from the query you entered.
 
*  Add some controls from the Data Controls tab.  For a quick check, add a TDBGrid component, and set its datasource property to the Datasource component you just added.  You should see the data from the query you entered.
  
OF course, this is just the beginning, but getting some data in a grid is usually the first step to ensure the database connection is working.  More detailed explanations will be found at [[Working With TSQLQuery]] and [[SQLDBHowto]].
+
Of course, this is just the beginning, but getting some data in a grid is usually the first step to ensure the database connection is working.  More detailed explanations will be found at [[Working With TSQLQuery]] and [[SQLDBHowto]].
  
  

Revision as of 11:24, 2 February 2013

English (en) español (es) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

The SQLdb package contains FPC units to access a number of SQL databases. It is "packaged" as sqldblaz.lpk in Lazarus, and the components can be found on the "SQLdb" tab.

Documentation

See SQLDB Documentation

Components

The SQLdb package provides the following components:

TXXXConnection

Documentation: see TSQLConnection odocumentation

There are various connection components called TXXXConnection, where XXX is the flavour of the database you are connecting to. Each one of these components takes the requests of the SQLQuery and SQLTransaction components and translates them into requests specifically tailored for the database you are using. These connection objects descend from the generic TSQLConnection; you can use TSQLConnection to create database programs that can connect to multiple databases (see SQLdb Tutorial3.

The actual components are:

  • TIBConnection (Borland Interbase / Firebird)
  • TODBCConnection (An ODBC connection to a database that the PC has the driver for; see ODBCConn)
  • TOracleConnection (Oracle)
  • TMySQL40Connection (MySQL, using MySQL 4.0 client library, perhaps earlier versions to - please confirm)
  • TMySQL41Connection (MySQL, using MySQL 4.1 client library)
  • TMySQL50Connection (MySQL, using MySQL 5.0 client library)
  • TMySQL51Connection (MySQL,available since FPC version 2.5.1)
  • TMySQL55Connection (MySQL,available since FPC version 2.6.1)
  • TPQConnection (PostgreSQL)
  • TSQLite3Connection (SQLite available since FPC version 2.2.2)
  • TMSSQLConnection (Microsoft SQL Server available since FPC version 2.6.1)
  • TSybaseConnection (Sybase ASE available since FPC version 2.6.1)

TSQLTransaction

Documentation: see TSQLTransaction documentation

This encapsulates the transaction on the database server. A TXXXConnection object always needs at least one TSQLTransaction associated with it, so that the transaction of its queries is managed. Queries/actions on the database need to be encapsulated in

  • a .StartTransaction/.Commit (or .Rollback) block
  • or a single .StartTransaction at the beginning, followed by .CommitRetaining or .RollBackretaiing. CommitRetaining and RollbackRetaining keep the transaction open, so performing another StartTransaction is not needed.

Setting .Active to true if it was false is equivalent to .StartTransaction.

Setting .Active to false if it was true is equivalent to .Rollback


TSQLQuery

Documentation: see TSQLQuery

This is a descendant of TDataset, and provides the data as a table from the SQL query that you submit. However, it can also be used to execute SQL queries (e.g. stored procedures, INSERT INTO..) that don't return any data. See Working With TSQLQuery for more details.

Using the SQLdb Package

To use the SQLdb package, you need a TSQLQuery component, a TSQLTransaction component, and one of the connection components.

Here is a quick tutorial:

  • Go to the SQLdb tab in Component Palette.
  • Add a TXXXConnection component (suitable for the type of database you are using) to a form, and fill in the appropriate properties to connect to your database. These will vary depending on the actual database. Make sure you can set the "connected" property to true.
  • Add a TSQLTransaction component. Return to the Connection component, and set its Transaction Property to the new transaction. This should also set the transaction's database property. You should be able to set active to true.
  • Add an SQLQuery component and set its Database property to the connecton component you just added.
  • Set the SQLQuery "SQL" property. For a first test, just do a "select * from <tablename>".
  • Set active to true. If this works, you have opened your query, and the data is available.

From here on, adding controls is the same as for any database, but just to recap:

  • Add a TDatasource component (from the Data Access tab) and set its dataset property to the SQLQuery component
  • Add some controls from the Data Controls tab. For a quick check, add a TDBGrid component, and set its datasource property to the Datasource component you just added. You should see the data from the query you entered.

Of course, this is just the beginning, but getting some data in a grid is usually the first step to ensure the database connection is working. More detailed explanations will be found at Working With TSQLQuery and SQLDBHowto.


See also