Difference between revisions of "SQLdb Package"

From Lazarus wiki
Jump to navigationJump to search
(→‎Notes: Removed old notes on specific databases; moved mysql stuff to mysql page. If code doesn't work, please specify link to bug report so it can be tracked properly)
(→‎TXXXConnection: Links to db specific pages)
Line 13: Line 13:
  
 
==== TXXXConnection ====
 
==== 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.  
+
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.  
  
 
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 ...)
+
*TODBCConnection (An ODBC connection to a database that the PC has the driver for; see [[ODBCConn]])
*TOracleConnection (Oracle)
+
*TOracleConnection ([[Lazarus_Database_Overview#Lazarus_and_Oracle|Oracle]])
*TMySQL40Connection (MySQL - various versions)
+
*TMySQL40Connection ([[mysql|MySQL]], using MySQL 4.0 client library, perhaps earlier versions to - please confirm)
*TMySQL41Connection
+
*TMySQL41Connection ([[mysql|MySQL]], using MySQL 4.1 client library)
*TMySQL50Connection
+
*TMySQL50Connection ([[mysql|MySQL]], using MySQL 5.0 client library)
*TMySQL51Connection (available since FPC version 2.5.1)
+
*TMySQL51Connection ([[mysql|MySQL]],available since FPC version 2.5.1)
*TMySQL55Connection (available since FPC version 2.6.1)
+
*TMySQL55Connection ([[mysql|MySQL]],available since FPC version 2.6.1)
*TPQConnection (PostgreSQL)
+
*TPQConnection ([[postgresql|PostgreSQL]])
*TSQLite3Connection (available since FPC version 2.2.2)
+
*TSQLite3Connection ([[SQLite|SQLite]] available since FPC version 2.2.2)
*TMSSQLConnection (available since FPC version 2.6.1)
+
*TMSSQLConnection ([[Lazarus_Database_Overview#Lazarus_and_MSSQL.2FSybase|Microsoft SQL Server]] available since FPC version 2.6.1)
*TSybaseConnection (available since FPC version 2.6.1)
+
*TSybaseConnection ([[Lazarus_Database_Overview#Lazarus_and_MSSQL.2FSybase|Sybase ASE]] available since FPC version 2.6.1)
  
 
== Using the SQLdb Package ==
 
== Using the SQLdb Package ==

Revision as of 13:02, 29 September 2012

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.

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. See Working With TSQLQuery for more details.

TSQLTransaction

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.

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.

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)

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 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:

  • 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.

Notes

More details can be found in SQLdb Programming Reference

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: http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=index-4&unit=sqldb