SqlDBHowto/pl

From Lazarus wiki
Revision as of 01:20, 2 October 2022 by Slawek (talk | contribs) (tłumaczenie na j. polski (niedokończone))
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) Nederlands (nl) polski (pl) 中文(中国大陆)‎ (zh_CN)

Databases portal

References:

Tutorials/practical articles:

Databases

Advantage - MySQL - MSSQL - Postgres - Interbase - Firebird - Oracle - ODBC - Paradox - SQLite - dBASE - MS Access - Zeos
Instrukcja do SqlDB

Ten tekst jest zaplanowany jako instrukcja w stylu „jak to zrobić”. Chcę odpowiedzieć na kilka pytań jedno po drugim i wyjaśnić, jak korzystać z różnych klas. Wszystkie te pytania są stawiane jedno po drugim i tworzą rodzaj samouczka.

Postaram się to sformułować w taki sposób, aby tekst mógł być użyty zarówno dla Lazarusa, jak i Free Pascala. Jednak przykłady dotyczą FreePascala (czyli są to aplikacje konsolowe).

Gdzie mogę znaleźć oficjalną dokumentację?

Proszę zapoznać się z oficjalną dokumentacją pod adresem dokumentacja SQLDB.

Jak połączyć się z serwerem bazy danych?

SqlDB nie łączy się bezpośrednio z serwerem bazy danych, ale używa klienta, który odpowiada używanemu serwerowi bazy danych. SqlDB wysyła polecenia do biblioteki klienta; biblioteka klienta łączy się z bazą danych i przekazuje polecenia. Oznacza to, że aby nawiązać połączenie z bazą danych, na komputerze musi być zainstalowana biblioteka klienta. W systemie Windows klientem jest zwykle biblioteka .dll, w Linuksie .so, a pod OS/X .dylib.

Gdy biblioteka klienta jest poprawnie zainstalowana, możesz połączyć się z serwerem bazy danych za pomocą komponentu TSQLConnection. Różne komponenty TSQLConnection są dostępne dla różnych serwerów baz danych (patrz SQLdb_Package):

Uwaga dotycząca MySQL - Istnieje wiele różnic pomiędzy wersjami klienta, w tym zakresie, że klienci i połączenia nie mogą być wymieniane. Jeśli zainstalowana jest biblioteka klienta MySQL w wersji 4.1, musisz użyć połączenia TMySQL41Connection. Nie jest to związane z serwerem MySQL; używając biblioteki klienckiej MySQL 4.1 możesz prawdopodobnie połączyć się z serwerem MySQL 5.0 (zobacz dokumentację MySQL, aby dowiedzieć się, jakie kombinacje są obsługiwane).

Chociaż różne bazach danych różnią się w szczegółach, to w praktyce musisz ustawić tylko cztery właściwości, aby połączyć się z serwerem bazy danych:

  • nazwa serwera lub adres IP
  • nazwa bazy danych
  • nazwa użytkownika
  • hasło

Gdy te właściwości są ustawione, możesz utworzyć połączenie za pomocą metody 'open'. Jeśli połączenie nie powiedzie się, zostanie zgłoszony wyjątek EDatabaseError. Użyj właściwości 'connected', aby sprawdzić, czy nawiązano połączenie z serwerem bazy danych. Użyj metody 'close', aby zakończyć połączenie z serwerem.

Program ConnectDB;

{$mode objfpc}{$H+}

uses
  IBConnection;

function CreateConnection: TIBConnection;
begin
  result := TIBConnection.Create(nil);
  result.Hostname := 'localhost';
  result.DatabaseName := '/opt/firebird/examples/employee.fdb';
  result.UserName := 'sysdba';
  result.Password := 'masterkey';
end;
 
var   
  AConnection : TIBConnection;

begin
  AConnection := CreateConnection;
  AConnection.Open;
  if Aconnection.Connected then
    writeln('Połączenie udało się!')
  else
    writeln('Wypisanie tego tekstu nie jest to możliwe! ' +
            'Ten kod nie zostanie wykonany, ponieważ w przypadku ' +
            'niepowodzenia połączenia zgłoszony zostanie wyjątek.');
  AConnection.Close;
  AConnection.Free;
end.

Jeśli zostanie zgłoszony wyjątek, uważnie przeczytaj komunikat o błędzie. Możliwe, że serwer bazy danych nie działa albo nazwa użytkownika, hasło, nazwa bazy danych lub adres IP są wpisane niepoprawnie. Jeśli komunikat o błędzie informuje, że nie można znaleźć biblioteki klienta, to sprawdź, czy klient jest poprawnie zainstalowany. Często komunikat o błędzie zawiera dosłownie nazwę szukanego pliku.

Jak wykonać bezpośrednie zapytania/utworzyć tabelę?

SqlDB - nazwa mówi wszystko - działa tylko z serwerem baz danych, który korzysta z SQL. SQL oznacza 'Structured Query Language' (czyli „Strukturalny język zapytań”). SQL to język opracowany w celu umożliwienia pracy z relacyjnymi bazami danych. Praktycznie każdy system baz danych ma swój własny dialekt, ale duża liczba instrukcji SQL jest taka sama dla wszystkich systemów baz danych.

W FPC wystepują pewne różnice pomiędzy:

  • Instrukcjami SQL, które zwracają informacje (zestaw danych - dataset). W tym celu musisz użyć komponentu TSQLQuery; patrz #Jak odczytać dane z tabeli?.
  • Instrukcjami, które nie zwracają informacji, ale robią coś innego, np. uaktualniają dane. W tym celu możesz również użyć metody 'ExecuteDirect' połączenia TSQLConnection. (Możesz również użyć tego, jeśli otrzymujesz w odpowiedzi zestaw danych, ale nie jesteś zainteresowany wynikami, np. w możliwej do wybrania procedurze składowanej).

Większość systemów baz danych wykonuje instrukcje SQL w ramach transakcji. Jeśli chcesz, aby zmiany dokonane w ramach transakcji były dostępne w innych transakcjach lub te zmiany były dostępne nawet po zamknięciu transakcji(!), musisz zatwierdzić transakcję instrukcją 'commit'.

Do obsługi transakcji w Sqldb służy komponent TSQLTransaction. Instrukcja SQL wykonywana przez Sqldb zawsze musi być wykonywana w ramach transakcji, nawet jeśli system bazy danych nie obsługuje transakcji. Ponadto istnieją systemy baz danych, które obsługują transakcje, ale dla których TSQLConnection nie obsługuje (jeszcze) transakcji. Nawet wtedy musisz użyć komponentu TSQLTransaction.

Aby użyć TSQLConnection.ExecuteDirect do wykonania instrukcji SQL, musisz określić, która transakcja ('Transaction') ma być użyta. Z kolei, aby skorzystać z TSQLTransaction musisz określić, który komponent TSQLConnection ma być użyty.

Poniższy przykład tworzy tabelę 'TBLNAMES' z polami 'NAME' i 'ID' oraz wstawia dwa rekordy. Tym razem przy użyciu SQLite. Użyte instrukcje SQL nie zostały wyjaśnione. Więcej informacji na temat instrukcji SQL, ich użycia i składni można znaleźć w dokumentacji systemu baz danych. Zauważ, że ten przykład nie próbuje wychwycić żadnych błędów, to bardzo źle! Zajrzyj do Wyjątki.

program CreateTable;
{$mode objfpc} {$ifdef mswindows}{$apptype console}{$endif}
uses
  sqldb, sqlite3conn; 
 
var 
  AConnection : TSQLite3Connection;
  ATransaction : TSQLTransaction;
 
begin
  AConnection := TSQLite3Connection.Create(nil);
  AConnection.DatabaseName := 'test_dbase';
 
  ATransaction := TSQLTransaction.Create(AConnection);
  AConnection.Transaction := ATransaction;
  AConnection.Open;
  ATransaction.StartTransaction;
  AConnection.ExecuteDirect('create table TBLNAMES (ID integer, NAME varchar(40));'); 
 
  ATransaction.Commit;
 
  ATransaction.StartTransaction;
  AConnection.ExecuteDirect('insert into TBLNAMES (ID,NAME) values (1,''Name1'');'); 
  AConnection.ExecuteDirect('insert into TBLNAMES (ID,NAME) values (2,''Name2'');'); 
  ATransaction.Commit; 
  AConnection.Close;
  ATransaction.Free;	
  AConnection.Free;
end.

Jak odczytać dane z tabeli?

Użyj komponentu TSQLQuery, aby odczytać dane z tabeli. Komponent TSQLQuery musi być połączony z komponentem TSQLConnection i komponentem TSQLTransaction, aby mógł poprawnie wykonać swoją pracę. Ustawienie TSQLConnection i TSQLTransaction zostało omówione w #Jak połączyć się z serwerem bazy danych? i #Jak wykonać bezpośrednie zapytania/utworzyć tabelę?.

Gdy TSQLConnection, TSQLTransaction i TSQLQuery są połączone, TSQLQuery wymaga dalszej konfiguracji do pracy. TSQLQuery ma właściwość 'SQL' zawierającą obiekt TStrings. Ta właściwość 'SQL' zawiera instrukcję SQL, która musi zostać wykonana. Jeśli wszystkie dane z tabeli nazwa_tabeli muszą zostać odczytane, ustaw właściwość 'SQL' na:

'SELECT * FROM nazwa_tabeli;'

.

Użyj metody 'open', aby odczytać tabelę z serwera i umieścić dane w zbiorze danych TSQLQuery. Dostęp do danych można uzyskać za pośrednictwem TSQLQuery, dopóki zapytanie nie zostanie zamknięte za pomocą metody 'close'.

TSQLQuery jest podklasą TDataset. TDataset posiada kolekcję 'Fields', która zawiera wszystkie kolumny tabeli. TDataset śledzi również bieżący rekord. Użyj metod 'First', 'Next', 'Prior' i 'Last', aby zmienić bieżący rekord. 'Bof' zwraca 'True', jeśli osiągnięty został pierwszy rekord, a 'Eof' zwraca 'True', jeśli osiągnięty zostanie ostatni rekord. Aby odczytać wartość pola w bieżącym rekordzie, najpierw znajdź odpowiedni obiekt 'TField', a następnie użyj 'AsString', 'AsInteger' itp.

Przykład: odczyt danych z tabeli

Poniżej znajduje się przykład, który wyświetla wszystkie wartości tabeli, tak jak została stworzona w powyższym przykładzie CreateTable. Skopiuj plik test_database do katalogu roboczego ShowData. Zauważ, że w tym przykładzie także nie przeprowadzono sprawdzania błędów!

Program ShowData;
 {$mode objfpc} {$ifdef mswindows}{$apptype console}{$endif}
uses
  DB, Sysutils, sqldb, sqlite3conn;

var
  AConnection  : TSQLConnection;
  ATransaction : TSQLTransaction;
  Query        : TSQLQuery;
 
begin
  AConnection := TSQLite3Connection.Create(nil);
  ATransaction := TSQLTransaction.Create(AConnection);
  AConnection.Transaction := ATransaction;
  AConnection.DatabaseName := 'test_dbase';
  Query := TSQLQuery.Create(nil);
  Query.SQL.Text := 'select * from tblNames';
  Query.Database := AConnection;
  Query.Open;
  while not Query.Eof do
  begin
    Writeln('ID: ', Query.FieldByName('ID').AsInteger, 'Name: ' +
                                  Query.FieldByName('Name').AsString);
    Query.Next;
  end;
  Query.Close;
  AConnection.Close;
  Query.Free;
  ATransaction.Free;
  AConnection.Free;
end.

(Powyższy kod oczywiście nie jest całkiem ukończony, bo brakuje mu bloków 'try...finally'. Jednak powyższy kod ma na celu pokazanie kodu obsługi bazy danych, a zatem pominięto ostatnie poprawki.) Należy pamiętać, że 'TSQLTransaction.StartTransaction' nie jest używany. To nie jest konieczne. Po otwarciu TSQLQuery, wykonywane jest polecenie SQL, a jeśli żadna transakcja nie jest dostępna, to transakcja jest uruchamiana automatycznie. Programista nie musi jawnie rozpoczynać transakcji. To samo dotyczy połączenia utrzymywanego przez TSQLConnection. Połączenie jest otwierane w razie potrzeby, wiersz 'Aconnection.Open' nie jest tak naprawdę wymagany. Jeśli transakcja TSQLTransaction zostanie zniszczona, zostanie wykonane automatyczne wycofanie. Ewentualne zmiany danych zawartych w transakcji zostaną utracone.

Dlaczego TSQLQuery.RecordCount zawsze zwraca wartość 10?

Aby policzyć rekordy w zbiorze danych, użyj '.RecordCount'. Zauważ jednak, że '.RecordCount' pokazuje liczbę rekordów, które zostały już załadowane z serwera. Ze względu na wydajność, SqlDB domyślnie nie odczytuje wszystkich rekordów podczas otwierania TSQLQuery, a tylko pierwsze 10. Dopiero po uzyskaniu dostępu do jedenastego rekordu zostanie załadowany następny zestaw 10 rekordów itd. Użycie metody '.Last' sprawi, że wszystkie rekordy zostaną załadowane.

Jeśli chcesz poznać rzeczywistą liczbę rekordów na serwerze, możesz najpierw wywołać '.Last', a następnie '.RecordCount'.

Dostępny jest też sposób alternatywny. Liczbę rekordów zwracanych przez serwer określa właściwość '.PacketRecords'. Wartość domyślna to 10; jeśli ustawisz ją na -1, to wszystkie rekordy zostaną załadowane jednocześnie.

W obecnej stabilnej wersji FPC '.RecordCount' nie uwzględnia filtrów, tj. pokazuje sumę niefiltrowanych rekordów.

Jeśli potrzebujesz dokładnej liczby rekordów, często lepszym pomysłem jest bezpośrednie zapytanie o liczbę rekordów w zapytaniu za pomocą innego zapytania SQL, ale musisz to zrobić w tej samej transakcji, ponieważ inne transakcje mogły zmienić liczbę rekordów w międzyczasie.

Lazarus

Lazarus posiada różne komponenty do wyświetlania danych z zestawu TDataset na formularzu. Zamiast instrukcji While-loop i Writeln, jak pokazano powyżej, można użyć komponentów do wyświetlenia danych w tabeli. Umieść na formularzu odpowiednie komponenty TSQLConnection, TSQLTransaction i TSQLQuery, a następnie połącz je i odpowiednio ustaw. Dodatkowo będziesz potrzebować TDatasource; ustaw właściwość 'TDatasource.Dataset' na używany komponent TSQLQuery. (Uwaga nie ustawiaj właściwości 'TSQLQuery.Datasource' na użyty komponent TDatasource. Właściwość 'TSQLQuery.Datasource' jest używana tylko w tabelach master-detail - patrz MasterDetail). Następnie możesz umieścić TDBGrid na formularzu i ustawić właściwość 'Datasource' siatki na komponent TDatasource, który dodałeś wcześniej.

Aby sprawdzić, czy wszystko działa, ustaw właściwość 'Connected' TSQLConnection na 'True' w Lazarus IDE. IDE natychmiast spróbuje połączyć się z serwerem bazy danych. Jeśli to zadziała, możesz ustawić właściwość 'TSQLQuery.Active' na 'True'. Jeśli wszystko jest w porządku, zobaczysz - w IDE - bezpośrednio na ekranie wszystkie dane z tabeli.

How to change data in a table?

To change the data in a record (or records), the general process is get TSQLQuery to search for the records you wish to change, make the changes there and then push them back to the database. The TDataSet (from which TSQLQuery is derived) must be set to edit mode. To enter edit mode call the '.Edit', '.Insert' or '.Append' methods. Use the '.Edit' method to change the current record. Use '.Insert' to insert a new record before the current record. Use '.Append' to insert a new record at the end of the table. In edit mode you can change field values through the 'Fields' property. Use 'Post' to validate the new data, if the data is valid then the edit mode is left. If you move to another record - for example by using '.Next' - and the dataset is in edit mode, then first '.Post' is called. Use '.Cancel' to discard all changes you made since the last '.Post' call and leave the edit mode.

Query.Edit;
Query.FieldByName('NAME').AsString := 'Edited name';
Query.Post;

The above is not the complete story yet. TSQLQuery is derived from TBufDataset which makes use of buffered updates. Buffered update means that after you called 'Post' the changes in the dataset are visible immediately, but they are not sent to the database server. What does happen is that the changes are maintained in a change log. When the '.ApplyUpdates' method is called, then all changes in the change log are sent to the database. Only then will database server know of the changes. The changes are sent to the server within a transaction of TSQLTransaction. Make sure to properly set the transaction before 'ApplyUpdates'. After applying the updates, a commit must be executed to save the changes on the database server.

The below is an example of changing the data in a table, sending the changes to the server and committing the transaction. Again, no error checking, again, thats bad!

Program EditData;
{$mode objfpc} {$ifdef mswindows}{$apptype console}{$endif}
uses
    db, sqldb, sqlite3conn;
var 
  AConnection : TSQLConnection;
  ATransaction : TSQLTransaction;
  Query : TSQLQuery;
 
begin
  AConnection := TSQLite3Connection.Create(nil);
  ATransaction := TSQLTransaction.Create(AConnection);
  AConnection.Transaction := ATransaction;
  AConnection.DatabaseName := 'test_dbase';
  Query := TSQLQuery.Create(nil);
  Query.DataBase := AConnection;
  Query.SQL.Text := 'select * from tblNames where ID = 2';
  Query.Open;
  Query.Edit;
  Query.FieldByName('NAME').AsString := 'Name Number 2';
  Query.Post;
  Query.UpdateMode := upWhereAll;         // defined in db
  Query.ApplyUpdates;
  ATransaction.Commit;
  Query.Free;
  ATransaction.Free;
  AConnection.Free;
end.

The actual works starts with the SQL statement "select * from tblNames where ID = 2" identifying the record (or records) you wish to change. If you leave out the "where ID = 2" bit, the TSQLQuery apparently sets ID (and other integer fields?) to 1. And therefore will operate on lines where ID=1 only. For a discussion of 'UpdateMode' continue reading.

How does SqlDB send the changes to the database server?

In the code example in #How to change data in a table?, you will find the line

Query.UpdateMode := upWhereAll;

without explanation of what it does. The best way to find out what that line does is to leave it out. If you leave out the statement and the followed this howto precisely, then you will receive the following error message:

No update query specified and failed to generate one. (No fields for inclusion in where statement found)

To understand what went wrong, you must understand how changes are sent to the database server. The only way to get data in a SQL server is by executing SQL queries. SQL has three types of queries for three different ways of manupulating a record. To create a new record, change or delete a record insert, update and delete statements are executed respectively. An update statement may be as follows:

update TBLNAMES set NAME='Edited name' where ID=1;

To send a change to the database server, Sqldb must assemble an update query. To assemble the query, three things are needed:

The name of the table
The table name is retrieved from parsing the select query, although this doesn't always work.
UPDATE or INSERT clause
These contain the fields that must be changed.
WHERE clause
This contains the fields that determine which records should be changed.

Every field (each TField in Fields) has a ProviderFlags property. Only fields with pfInUpdate in ProviderFlags will be used in the update or insert cluase of a query. By default all fields have pfInUpdate set in their ProviderFlags property.

Which fields are used in the WHERE clause depends on the UpdateMode property of the query and the ProviderFlags property of the fields. Fields with pfInkey in their ProviderFlags are always used in the WHERE clause. A field will have the pfInKey flag set automatically if the field is part of the primary key of the table and 'TSQLQuery.UsePrimaryKeyAsKey' returns 'True'.

The default value for UpdateMode of the query is upWhereKeyOnly. In this update mode only fields with pfInkey in their ProviderFlags property are used in the WHERE clause. If none of the fields have their pfInKey flag set, then no fields are available for the WHERE clause and the error message from the beginning of this section will be returned. You can solve the issue by:

  • Adding a primary key to the table and set TSQLQuery.UsePrimaryKeyAsKey to 'True', or
  • Setting the pfInkey flag for one or more fields in code.

The UpdateMode property knows two more possible values. 'upWhereAll' can be used to add all fields with the 'pfInWhere' flag set to the WHERE clause. By default all fields have this flag set. 'upWhereChanged' can be used to add only those fields that have the 'pfInWhere' flag set and that are changed in the current record.

How to handle Errors

Run time errors are unavoidable, disks may fill up, necessary libraries or helper apps may not be available, things go wrong and we need to allow for that. The FPC detects and handles run time errors quite well. It usually gives you a concise and reasonable explanation of what went wrong. However, you will want to monitor and handle errors yourself for a number of reasons -

  • You probably don't want the programme to teminate at the first sign of trouble.
  • If we do keep going, lets make sure any memory allocated in the problem area is recovered, we don't want any memory leaks.
  • If we are going to go under however, lets give the user a context sensitive explanation.

The following bit of code is based on the above examples but this time it DOES check for errors in critical places. Key is the try...finally...end and try...except...end blocks. You can test it by doing things like uninstalling SQLite3, putting a dummy file in place of the test_dbase database and so on.

program DemoDBaseWithErrors;
{$mode objfpc} {$ifdef mswindows}{$apptype console}{$endif}
uses
  DB, Sysutils, sqldb, sqlite3conn;
var
    Connect : TSQLite3Connection;
    Trans : TSQLTransaction;


procedure WriteTable (Command : string);
begin
	Connect.ExecuteDirect(Command);
    	Trans.Commit;
end;

procedure ReadTable ();
var
   Query : TSQLQuery;
   Count : smallint;
begin
    Count := 0;
    try
        Query := TSQLQuery.Create(nil);
        Query.DataBase := Connect;
        Query.SQL.Text:= 'select * from tblNames';
        Query.Open;          // This will also open Connect
        while not Query.EOF do begin
            writeln('ID: ', Query.FieldByName('ID').AsInteger, '  Name: ' +
                              Query.FieldByName('Name').AsString);
            Query.Next;
            Count := Count + 1;
        end;
    finally
        Query.Close;
        Query.Free;
    end;
    writeln('Found a total of ' + InttoStr(Count) + ' lines.');
end;

procedure FatalError(ClassName, Message, Suggestion : string);
begin
    writeln(ClassName);
    writeln(Message);
    writeln(Suggestion);
    Connect.Close;              // Its possibly silly worrying about freeing
    Trans.free;                 // if we are going to call halt() but its
    Connect.Free;               // a demo, alright ?
    halt();
end;

begin
    Connect := TSQLite3Connection.Create(nil);
    Trans := TSQLTransaction.Create(Connect);
	Connect.Transaction := Trans;
    Connect.DatabaseName := 'test_dbase';
    try
        if not fileexists(Connect.DatabaseName) then begin
            Connect.Open;   // give EInOutError if (eg) SQLite not installed
            Trans.StartTransaction;
            WriteTable('create table TBLNAMES (ID integer Primary Key, NAME varchar(40));');
            Trans.Commit;
        end;
        Connect.open;
        Trans.StartTransaction;
        WriteTable('insert into TBLNAMES (NAME) values (''AName1'');');
        WriteTable('insert into TBLNAMES (NAME) values (''AName2'');');
    except
        on E : EDatabaseError do
            FatalError(E.ClassName, E.Message, 'Does the file contain the correct database ?');
        on E : EInOutError do
            FatalError(E.ClassName, E.Message, 'Have you installed SQLite (and dev package)?');
        on E : Exception do
            FatalError(E.ClassName, E.Message, 'Something really really bad happened.');
     end;
    ReadTable();
    Connect.Close;
    Trans.Free;
    Connect.Free;
end.

How to execute a query using TSQLQuery?

Next to statements that return a dataset (see #Jak odczytać dane z tabeli?) SQL has statements that do not return data. For example INSERT, UPDATE and DELETE statements do not return data. These statements can be executed using TSQLConnection.ExecuteDirect, but TSQLQuery can also be used. If you do not expect return data use TSQLQuery.ExecSQL instead of TSQLQuery.Open. As mentioned earlier, use TSQLQuery.Open to open the dataset returned by the SQL statement.

The following procedure creates a table and inserts two records using TSQLQuery.

procedure CreateTable;
  
var 
  Query : TSQLQuery;
  
begin
  Query := TSQLQuery.Create(nil);
  try
    Query.Database := AConnection;

    Query.SQL.Text := 'create table TBLNAMES (ID integer, NAME varchar(40));';
    Query.ExecSQL;
 
    Query.SQL.Text := 'insert into TBLNAMES (ID,NAME) values (1,''Name1'');';
    Query.ExecSQL;
  
    Query.SQL.Text := 'insert into TBLNAMES (ID,NAME) values (2,''Name2'');';
    Query.ExecSQL;
  finally
    Query.Free;
  end;
end;

How to use parameters in a query?

In the code example of #How to execute a query using TSQLQuery? the same query is used twice, only the values to be inserted differ. A better way to do this is by using parameters in the query.

The syntax of parameters in queries is different per database system, but the differences are handled by TSQLQuery. Replace the values in the query with a colon followed by the name of the parameter you want to use. For example:

Query.SQL.Text := 'insert into TBLNAMES (ID,NAME) values (:ID,:NAME);';

This query will create two parameters: 'ID' and 'NAME'. To determine the parameters, the query is parsed at the moment the text of TSQLQuery.SQL is assigned or changed. All existing parameters will be removed and the new parameters will be added to the 'TSQLQuery.Params' property. Assigning a value to a parameter is similar to assigning a value to a field in the dataset:

Query.Params.ParamByName('Name').AsString := 'Name1';

You can't tell from the query what kind of data must be stored in the parameter. The data type of the parameter is determined at the moment a value is first assigned to the parameter. By assigning a value using '.AsString', the parameter is assigned the data type 'ftString'. You can determine the data type directly by setting the 'DataType' property. If an incorrect datatype is assigned to the parameter, then problems will occur during opening or executing the query. See Database field type for more information on data types.

Select query

An example of a select query with parameters would be to change something like this:

  Query.SQL.Text := 'select ID,NAME from TBLNAMES where NAME = '''+Edit1.Text+''' ORDER BY NAME ';

to something like this:

  Query.SQL.Text := 'select ID,NAME from TBLNAMES where NAME = :NAMEPARAM ORDER BY NAME ';
  Query.Params.ParamByName('NAMEPARAM').AsString := Edit1.Text;

Example

The following example creates the same table as the previous example, but now parameters are used:

procedure CreateTableUsingParameters;
  
var 
  Query : TSQLQuery;
  
begin
  Query := TSQLQuery.Create(nil);
  try
    Query.Database := AConnection;

    Query.SQL.Text := 'create table TBLNAMES (ID integer, NAME varchar(40));';
    Query.ExecSQL;

    Query.SQL.Text := 'insert into TBLNAMES (ID,NAME) values (:ID,:NAME);';
    Query.Prepare;
  
    Query.Params.ParamByName('ID').AsInteger := 1;
    Query.Params.ParamByName('NAME').AsString := 'Name1';
    Query.ExecSQL;
  
    Query.Params.ParamByName('ID').AsInteger := 2;
    Query.Params.ParamByName('NAME').AsString := 'Name2';
    Query.ExecSQL;

    //Query.UnPrepare; // no need to call this; should be called by Query.Close
    Query.Close;
  finally
    Query.Free;
  end;
end;

Notice that this example requires more code than the example without the parameters. Then what is the use of using parameters?

Speed is one of the reasons. The example with parameters is faster, because the database server parses the query only once (in the .Prepare statement or at first run).

Another reason to use prepared statements is prevention of SQL-injection (see also Secure programming.

Finally, in some cases it just simplifies coding.

Troubleshooting: TSQLConnection logging

You can let a TSQLConnection log what it is doing. This can be handy to see what your Lazarus program sends to the database exactly, to debug the database components themselves and perhaps to optimize your queries. NB: if you use prepared statements/parametrized queries (see section above), the parameters are often sent in binary by the TSQLConnection descendent (e.g. TIBConnection), so you can't just copy/paste the logged SQL into a database query tool. Regardless, connection logging can give a lot of insight in what your program is doing.

Alternatives are:

  1. you can use the debugger to step through the database code if you have built FPC (and Lazarus) with debugging enabled.
  2. if you use ODBC drivers (at least on Windows) you could enable tracelog output in the ODBC control panel.
  3. many databases allow you to monitor all statements sent to it from a certain IP address/connection.

If you use TSQLConnection logging, two things are required:

  1. indicate which event types your TSQLConnection should log
  2. point TSQLConnection at a function that receives the events and processes them (logs them to file, prints them to screen, etc.).

That function must be of type TDBLogNotifyEvent (see sqldb.pp), so it needs this signature:

TDBLogNotifyEvent = Procedure (Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String) of object;

FPC (or: the manual way)

A code snippet can illustrate this:

uses
...
TSQLConnection, //or a child object like TIBConnection, TMSSQLConnection
...
var
type 
  TMyApplication = class(TCustomApplication); //this is our application that uses the connection
...
  private
    // This example stores the logged events in this stringlist:
    FConnectionLog: TStringList;
...
  protected
    // This procedure will receive the events that are logged by the connection:
    procedure GetLogEvent(Sender: TSQLConnection; EventType: TDBEventType; Const Msg : String);
...
  procedure TMyApplication.GetLogEvent(Sender: TSQLConnection;
    EventType: TDBEventType; const Msg: String);
  // The procedure is called by TSQLConnection and saves the received log messages
  // in the FConnectionLog stringlist
  var
    Source: string;
  begin
    // Nicely right aligned...
    case EventType of
      detCustom:   Source:='Custom:  ';
      detPrepare:  Source:='Prepare: ';
      detExecute:  Source:='Execute: ';
      detFetch:    Source:='Fetch:   ';
      detCommit:   Source:='Commit:  ';
      detRollBack: Source:='Rollback:';
      else Source:='Unknown event. Please fix program code.';
    end;
    FConnectionLog.Add(Source + ' ' + Msg);
  end;

...
  // We do need to tell our TSQLConnection what to log:
    FConnection.LogEvents:=LogAllEvents; //= [detCustom, detPrepare, detExecute, detFetch, detCommit, detRollBack]
    // ... and to which procedure the connection should send the events:
    FConnection.OnLog:=@Self.GetLogEvent;
...
  // now we can use the connection and the FConnectionLog stringlist will fill with log messages.

You can also use TSQLConnection's GlobalDBLogHook instead to log everything from multiple connections.

Lazarus (or: the quick way)

Finally, the description above is the FPC way of doing things as indicated in the introduction; if using Lazarus, a quicker way is to assign an event handler to the TSQLConnection's OnLog event.

See also