Difference between revisions of "SQLdb Package"

From Lazarus wiki
Jump to navigationJump to search
m (Moved the notes-section to the bottom)
m (TMSSQLConnection added)
 
(25 intermediate revisions by 7 users not shown)
Line 1: Line 1:
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.
+
{{SQLdb Package}}
==Components==
+
 
 +
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]].
 +
 
 +
[[File:sqldbcomponents.png‎]]
 +
 
 +
== Documentation ==
 +
See [http://www.freepascal.org/docs-html/fcl/sqldb/index.html SQLDB Documentation]
 +
 
 +
== Components ==
 
The SQLdb package provides the following components:
 
The SQLdb package provides the following components:
====TSQLQuery====
 
This is a descendant of TDataset, and provides the data as a table from the SQL query that you submit. But can also be used to execute SQL-queries that don't return any data.
 
  
====TSQLTransaction====
+
==== TXXXConnection ====
This encapsulates the transaction on the database server. I am not going to say much about it as I haven't quite worked out the details myself!  The important bit is that it has the commit and rollback methods to finish or cancel your transaction on the database.
+
Documentation: see [http://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.html TSQLConnection documentation]
====TXXXConnection====
+
 
where XXX is the flavour of the database you are connecting to. Each one of these components takes the "standard" requests of the SQLQuery and SQLTransaction components and translates them into database requests, allowing for the idiosyncrasies of the specific 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]].
  
 
The actual components are:
 
The actual components are:
*TIBConnection (Borland Interbase / Firebird)
+
*TIBConnection ([[Firebird|Borland Interbase / Firebird]])
*TODBCConnection (An ODBC connection to a database that the PC has the driver for ...)
+
*TMSSQLConnection ([[mssqlconn|Microsoft SQL Server]], available since FPC version 2.6.1/Lazarus 1.0.8)
*TOracleConnection (Oracle)
+
*TMySQL40Connection ([[mysql|MySQL]], requires MySQL 4.0 client library)
*TMySQL40Connection (MySQL - various versions)
+
*TMySQL41Connection ([[mysql|MySQL]], requires MySQL 4.1 client library)
*TMySQL41Connection
+
*TMySQL50Connection ([[mysql|MySQL]], requires MySQL 5.0 client library)
*TMySQL50Connection
+
*TMySQL51Connection ([[mysql|MySQL]], requires MySQL 5.1 client library, available since FPC version 2.5.1)
*TPQConnection (PostgreSQL)
+
*TMySQL55Connection ([[mysql|MySQL]], requires MySQL 5.5 client library, available since FPC version 2.6.1/Lazarus 1.0.8)
*TSQLite3Connection (fpc version 2.2.2)
+
*TODBCConnection (An ODBC connection to a database that the PC has the driver for; see [[ODBCConn]])
 +
*TOracleConnection ([[Lazarus_Database_Overview#Lazarus_and_Oracle|Oracle]])
 +
*TPQConnection ([[postgresql|PostgreSQL]])
 +
*TSybaseConnection ([[mssqlconn|Sybase ASE]], available since FPC version 2.6.1/Lazarus 1.0.8)
 +
*TSQLite3Connection ([[SQLite|SQLite]], available since FPC version 2.2.2)
 +
*TMSSQLConnection ([[mssqlconn|MSSQL]], requires FreeTDS library)
  
==Using the SQLdb Package==
+
==== 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.
 +
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 [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.
 +
 +
== Using the SQLdb Package ==
 
To use the SQLdb package, you need a TSQLQuery component, a TSQLTransaction component, and one of the connection components.
 
To use the SQLdb package, you need a TSQLQuery component, a TSQLTransaction component, and one of the connection components.
 
   
 
   
 
Here is a quick tutorial:
 
Here is a quick tutorial:
 +
*  Make sure you have the relevant database client/driver installed.
 +
*  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.
  
*  Go to the SQLdb tab in Component Palette.
 
*  Add a T___Connection 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 - check to make sure.  You should be able to set active to true.
 
*  Add an SQLQuery component and set its Database and Transaction properties to the components you just added.
 
*  Set the SQLQuery "SQL" property.  For a first test, just do a "select * from <tablename>.
 
*  Set active to true.  If you can, 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:  
 
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 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.
 
*  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]].
+
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 [[SQLdb Tutorial1]].
 
 
==Notes==
 
More details will be found in [[SQLdb Programming Reference]]
 
 
 
The pages related to the SQLdb package were initially written from explorations done using TPQConnection, using a PostgreSQL 8.1 database on Win XP O/S.  They are expected to be true for the other databases and OSes as well.  If you can confirm this, please leave a note here.
 
 
 
It seems to work similarly on MySQL 5.0.45 on Win XP. TSQLTransaction property 'active' cannot however be set to true, but the live data from the database table are visible in the TDbGrid component already during design of the form and the program compiles.
 
 
 
There are some notes and documentation started:
 
http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=fcldbnotes
 
  
An interface view of the classes is availble:
+
== See also ==
http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=index-4&unit=sqldb
+
* [[SqlDBHowto]]
 +
* [[SQLdb Programming Reference]]
 +
* [[Databases]]
 +
* [http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=index-4&unit=sqldb interface view of the classes]
 +
* [http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=fcldbnotes some additional documentation/notes]

Latest revision as of 15:02, 25 June 2018

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.

sqldbcomponents.png

Documentation

See SQLDB Documentation

Components

The SQLdb package provides the following components:

TXXXConnection

Documentation: see TSQLConnection documentation

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)
  • TMSSQLConnection (Microsoft SQL Server, available since FPC version 2.6.1/Lazarus 1.0.8)
  • TMySQL40Connection (MySQL, requires MySQL 4.0 client library)
  • TMySQL41Connection (MySQL, requires MySQL 4.1 client library)
  • TMySQL50Connection (MySQL, requires MySQL 5.0 client library)
  • TMySQL51Connection (MySQL, requires MySQL 5.1 client library, available since FPC version 2.5.1)
  • TMySQL55Connection (MySQL, requires MySQL 5.5 client library, available since FPC version 2.6.1/Lazarus 1.0.8)
  • TODBCConnection (An ODBC connection to a database that the PC has the driver for; see ODBCConn)
  • TOracleConnection (Oracle)
  • TPQConnection (PostgreSQL)
  • TSybaseConnection (Sybase ASE, available since FPC version 2.6.1/Lazarus 1.0.8)
  • TSQLite3Connection (SQLite, available since FPC version 2.2.2)
  • TMSSQLConnection (MSSQL, requires FreeTDS library)

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:

  • Make sure you have the relevant database client/driver installed.
  • 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 SQLdb Tutorial1.

See also