Sign in Join
Wiki copytemporary

SQLdb Package: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The 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]].
22
3-<figure>
4-<img src="sqldbcomponents.png‎" title="sqldbcomponents.png‎" />
5-<figcaption>sqldbcomponents.png‎</figcaption>
6-</figure>
3+![](/wiki-images/c/c5/Sqldbcomponents.png%E2%80%8E)
74
85## Documentation
96
107See [SQLDB Documentation](http://www.freepascal.org/docs-html/fcl/sqldb/index.html)
118
129## Components
1310
1411The SQLdb package provides the following components:
1512
1613#### TXXXConnection
1714
1815Documentation: see [TSQLConnection documentation](http://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.html)
1916
2017There 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]].
2118
2219The actual components are:
2320
2421- TIBConnection ([[Firebird|Borland Interbase / Firebird]])
2522- TMSSQLConnection ([[mssqlconn|Microsoft SQL Server]], available since FPC version 2.6.1/Lazarus 1.0.8)
2623- TMySQL40Connection ([[mysql|MySQL]], requires MySQL 4.0 client library)
2724- TMySQL41Connection ([[mysql|MySQL]], requires MySQL 4.1 client library)
2825- TMySQL50Connection ([[mysql|MySQL]], requires MySQL 5.0 client library)
2926- TMySQL51Connection ([[mysql|MySQL]], requires MySQL 5.1 client library, available since FPC version 2.5.1)
3027- TMySQL55Connection ([[mysql|MySQL]], requires MySQL 5.5 client library, available since FPC version 2.6.1/Lazarus 1.0.8)
3128- TODBCConnection (An ODBC connection to a database that the PC has the driver for; see [[ODBCConn]])
3229- TOracleConnection ([[Lazarus Database Overview|Oracle]])
3330- TPQConnection ([[postgresql|PostgreSQL]])
3431- TSybaseConnection ([[mssqlconn|Sybase ASE]], available since FPC version 2.6.1/Lazarus 1.0.8)
3532- TSQLite3Connection ([[SQLite]], available since FPC version 2.2.2)
3633- TMSSQLConnection ([[mssqlconn|MSSQL]], requires FreeTDS library)
3734
3835#### TSQLTransaction
3936
4037Documentation: see [TSQLTransaction documentation](http://www.freepascal.org/docs-html/fcl/sqldb/tsqltransaction.html)
4138
4239This 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
4340
4441- a .StartTransaction/.Commit (or .Rollback) block
4542- 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.
4643
4744Setting .Active to true if it was false is equivalent to .StartTransaction.
4845
4946Setting .Active to false if it was true is equivalent to .Rollback
5047
5148#### TSQLQuery
5249
5350Documentation: see [TSQLQuery](http://www.freepascal.org/docs-html/fcl/sqldb/tsqlquery.html)
5451
5552This 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.
5653
5754## Using the SQLdb Package
5855
5956To use the SQLdb package, you need a TSQLQuery component, a TSQLTransaction component, and one of the connection components.
6057
6158Here is a quick tutorial:
6259
6360- Make sure you have the relevant database client/driver installed.
6461- Go to the SQLdb tab in Component Palette.
6562- 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.
6663- 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.
6764- Add an SQLQuery component and set its Database property to the connecton component you just added.
6865- Set the SQLQuery "SQL" property. For a first test, just do a "select * from \<tablename\>".
6966- Set active to true. If this works, you have opened your query, and the data is available.
7067
7168From here on, adding controls is the same as for any database, but just to recap:
7269
7370- Add a TDatasource component (from the Data Access tab) and set its dataset property to the SQLQuery component
7471- 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.
7572
7673Of 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]].
7774
7875## See also
7976
8077- [[SqlDBHowto]]
8178- [[SQLdb Programming Reference]]
8279- [[Databases]]
8380- [interface view of the classes](http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=index-4&unit=sqldb)
8481- [some additional documentation/notes](http://z505.com/cgi-bin/powtils/docs/1.6/idx.cgi?file=fcldbnotes)
8582
8683---
8784*From the Free Pascal wiki, page "SQLdb Package", as it stood on 2018-06-25 after a change by Af0815, 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).*

Back to history