mysql/fr

From Lazarus wiki
Jump to navigationJump to search

English (en) español (es) français (fr) polski (pl)

Portail de la base de données

Références:

Tutoriels/articles pratiques :

Bases de données

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

Vue d'ensemble

Vous pouvez utiliser Free Pascal/Lazarus pour accéder à un serveur de base de données MySQL. Aussi vous pouvez utiliser les composant MySQL Data Access (MyDAC) comme un composant pour connecter Lazarus à MySQL. Les composants Lazarus MyDAC sont libres en téléchargement.

Win64: veuillez lire les avertissements ici sur l'inadéquation de certaines versions Win64 de FPC/Lazarus.

Avantages de MySQL:

  • Il est très largement utilisé et disponible.
  • Bien que les plus anciennes versions avaient la réputation de ne pas être de vrais SGBDR, les nouvelles versions prennent en charge les propriétés ACID si correctement configurées (avec le bon stockage en arrière plan (backend storage))

Inconvénients de MySQL:

  • Les mainteneurs de MySQL ne conservent pas la compatibilité entre les versions de bibliothèque client. Ce qui signifie que la traduction en FPC/Lazarus doit être faite pour chaque nouvelle version, ce qui ralentit les choses.
  • La licence peut être restrictive pour certaines utilisations (p.ex. les déploiements commerciaux). Il existe une alternative comme MariaDB compatible et sasn doute moins restrictive.

De nombreux utilisateurs de Lazarus/FPC préfèrent les SGBDR Firebird ou PostgreSQL pour cette raison.

Licence de MySQL

Avant un déploiement significatif de MySQL, n'oubliez pas de lire sa licence.

SQLDB

Lazarus 1.2 (with FPC 2.6.2), supports

  • Bibliothèque cliente MySQL 4.0
  • Bibliothèque cliente MySQL 4.1
  • Bibliothèque cliente MySQL 5.0
  • Bibliothèque cliente MySQL 5.1
  • Bibliothèque cliente MySQL 5.5 (pris en charge depuis Lazarus 1.0.8)
  • Bibliothèque cliente MySQL 5.6 (pris en charge depuis Lazarus <à remplir>)

sqldbcomponents.png

De plus récentes versions de FPC/Lazarus peuvent supporter de plus récentes bibliothèques clientes de MySQL.

Assurez-vous que vous utilisez le composant de connexion correct pour votre version de bibliothèque client. Ainsi si vous avez les bibliothèques clientes installées pour MySQL 4.1, vous devez utiliser le composant TMySQL41Connection, même si le serveur fonctionne en version 4.0 ou 5.0. La raison à cela est que les bibliothèques clientes MySQL casse souvent la compatibilité de leurs API et donc chaque version demande un pilote Pascal différent.

Sur les systèmes *nix, le code SQLdb peut rechercher la version pure de la bibliothèque sans suffixe de numéro de version. Il y a plusieurs manières de pouvoir traiter ceci :

  • (sur plusieurs Linux) installer la version -dev de la bibliothèque.
  • créer un lien symbolique sur le nom de bibliothèque spécifique vers celui que recherche FPC (plutôt que hacker)
  • utiliser TSQLDBLibraryLoader pour spécifier le nom de bibliothèque avant le chargement de la connexion.

Tutoriels SQLDB et exemple de code

Faire fonctionner MySQL dans Linux ou Windows

Suivez les instructions dans le Manuel Utilisateur de MySQL. Assurez-vous que le daemon mysqld tourne de manière fiable, que tous les utilisateurs potentiels (y compris root, mysql, vous-mêmes et quiconque en aurait besoin) aient les priilèges qu'ils exigent, de n'importe quel hôte qu'il y a besoin (tel que 'localhost', le nom d'hôte local, et d'autres hôte sur votre réseau) tout en restant cohérent avec la sécurité. Il est préférable que les tous les utilisateurs, y compris le root, (NdT : sic) aient des mots de passe. Testez l'action du système de base de donnée en utilisant les exemples donnés dans le manuel et contrôlez que tous les utilisateurs ont des accès fiables.

Faire fonctionner MySQL avec FPC en mode texte

Il y a un répertoire avec un programme d'exemple dans $(fpcsrcdir)/packages/base/mysql/. Vous pouvez trouver le répertoire des sources dans Lazarus: Tools -> Options -> Files -> FPC source directory. Les chemins possibles pour le répertoire mysql sont /usr/share/fpcsrc/packages/base/mysql/ (installation par rpm) ou C:\lazarus\fpcsrc\packages\base\mysql\ (windows). Ce répertoire contient aussi les unités mysql.pp, mysql_com.pp et mysql_version.pp. Avant d'exécuter le script de test, vous avez besoin une base de données nommée testdb: faites ceci en vous connectant au moniteur mysql (en root avec tous les privilèges) et lancer les ordres SQL suivants :

CREATE DATABASE testdb;

puis assurez-vous que tous les utilisateurs concernés ont les privilèges d'accès appropriés pour cela :

GRANT ALL ON testdb TO johnny-user IDENTIFIED BY 'johnnyspassword';

Il y a un script appelé mkdb que vous devriez maintenant essayer de lancer :

sh ./mkdb

Cela échouera probablement, car le système ne permet pas à un utilisateur anonyme d'accéder à la base de données. Il faut donc changer le script en utilisant un éditeur à la ligne :

mysql -u root -p  ${1-testdb} << EOF >/dev/null

et essayez d'exécuter à nouveau en entrant votre mot de passe à l'invite. Avec de la chance, vous avez peut-être réussi à créer la base de données de test : testez-la (tant que vous être connectés dans le moniteur mysql) en lançant l'ordre mysql

select * from FPdev;

Vous devriez voir une table listant l'ID, le nom d'utilisateur et l'adresse e-mail de quelques développeurs FPC. Maintenant, essayer d'exécuter le programme de test testdb.pp (il peut avoir besoin d'être compilé, et échouera certainement au premier essai !!).

J'ai trouvé que le programme ne pourrait pas se connecter à mysql pourt plusieurs raisons :

  • Mon système (SuSE Linux v9.0) installe mysql v4.0.15, pas la version 3 pour laquelle le paquet a été conçu.
  • Le programme a besoin d'avoir les noms d'utilisateur et les mots de passe pour obtenir l'accès à la base de données.
  • Le compilateur demande à savoir où trouver les bibliotèques mysql (SI VOUS N'AVEZ PAS INSTALLER LES BIBLIOTHEQUES DE DEV, FAITES-LE MAINTENANT !)

J4ai créé une copie de testdb.pp appelée trydb.pp, plutôt qu'éditer l'original - cela veut dire que les fichiers originaux peuvent encore être corrigés par les mise à jour de CVS. J'ai aussi copié les fichiers trouvés dans le sous-répertoire mysql/ver40/ dans le sous-répertoire mysql/, en les renommant mysql_v4.pp, mysql_com_v4.pp et mysql_version_v4.pp, en adaptant les nomes d'unités dans chacun des fichiers en conséquence. J'ai changé l'instruction uses dans trydb.pp en

uses mysql_v4

et l'instruction dans mysql_v4.pp en

uses mysql_com_v4

J'ai ajouté une ligne à /etc/fpc.cfg pour pointer vers mes bibliothèques :

-Fl/lib;/usr/lib
 * * * A FINIR * * * 

The following step might not be necessary if the devel-libraries are installed as the links will be created for you, but it never hurts to check. I had to find the real name of the mysqlclint library in the /usr/lib directory and in my case I had to issue the shell command:

ln -s libmysqlclient.so.12.0.0 lmysqlclient

to make a symbolic link allowing FPC to find the library. For good measure I also created the link

ln -s libmysqlclient.so.12.0.0 mysqlclient

and placed similar links in various other directories: not strictly necessary, but just in case ...! Some users might need to add the following link:

ln -s libmysqlclient.so.12.0.0 libmysqlclient.so

I modified trydb.pp to include user details, initially by adding host, user and password as constants:

const
  host : Pchar= 'localhost';
  user : Pchar= 'myusername';
  passwd: Pchar = 'mypassword';
Warning-icon.png

Avertissement: This section looks extremely outdated. If you are still on MySQL 4 perhaps it is time to upgrade

I also found that I couldn't connect to mysql using the mysql_connect() call, but had to use mysql_real_connect() which has many more parameters. To complicate things further, the number of parameters seems to have changed between version3 (where there are seven) and version4 (where there are eight). Before using mysql_real_connect I had to use mysql_init() which is not found in the original mysql.pp but is found in mysql_v4.pp.

So the code for connection to the database is now:

{ a few extra variables}
var
  alloc : PMYSQL;
  
{main program fragment}
  
begin
  if paramcount=1 then
  begin
    Dummy:=Paramstr(1)+#0;
    DataBase:=@Dummy[1];
  end;
  
  Writeln('Allocating Space...');
  alloc := mysql_init(PMYSQL(@qmysql));
  Write('Connecting to MySQL...');
  sock := mysql_real_connect(alloc, host, user, passwd, database, 0, nil, 0);
  if sock=Nil then
  begin
    Writeln(stderr,'Couldn''t connect to MySQL.');
    Writeln(stderr, 'Error was: ', mysql_error(@qmysql));
    halt(1);
  end;
  Writeln('Done.');
  Writeln('Connection data:');
 {$ifdef Unix}
  writeln('Mysql_port      : ',mysql_port);
  writeln('Mysql_unix_port : ',mysql_unix_port);
 {$endif}
  writeln('Host info       : ',mysql_get_host_info(sock));
  writeln('Server info     : ',mysql_stat(sock));
  writeln('Client info     : ',mysql_get_client_info);
  
  Writeln('Selecting Database ',DataBase,'...');
  if mysql_select_db(sock, DataBase) < 0 then
  begin
    Writeln(stderr,'Couldn''t select database ',Database);
    Writeln(stderr,mysql_error(sock));
    halt(1);
  end;
 {... as original contents of testdb.pp}

Now - ready to start compiling trydb.pp?

 fpc trydb

success! Now run it:

 ./trydb

whoopee! I got the listing of the FPC developers!

A few extra refinements: make the entry of user details and the mysql commands interactive, using variables rather than constants, and allow several SQL commands to be entered, until we issue the quit command: see the full program listing, where user details are entered from the console, and the program goes into a loop where SQL commands are entered from the console (without the terminal semicolon) and the responses are printed out, until 'quit' is entered from the keyboard.

See Sample Console Listing.

Se connecter à MySQL depuis une application Lazarus

This tutorial shows how to connect Lazarus to the MySQL database, and execute simple queries, using only the basic Lazarus components; it uses no Data Aware components, but illustrates the principles of interfacing with the database.

Create a new project in Lazarus:

Project -> New Project -> Application

A new automatically generated Form will appear.

Enlarge the form to fill about half of the screen, then re-name the form and its caption to 'TryMySQL'.

From the Standard Component tab place three Edit Boxes on the upper left side of the Form, and immediately above each box place a label. Change the names and captions to 'Host' (and HostLLabel,HostEdit), 'UserName' (and UserLabel, UserEdit) and 'Password' (with PasswdLabel and PasswdEdit). Alternatively you could use LabelledEdit components from the Additional tab.

Select the Passwd Edit box and find the PasswordChar property: change this to * or some other character, so that when you type in a password the characters do not appear on your screen but are echoed by a series of *s. Make sure that the Text property of each edit box is blank.

Now place another Edit box and label at the top of the right side of your form. Change the label to 'Enter SQL Command' and name it CommandEdit.

Place three Buttons on the form: two on the left under the Edit boxes, and one on the right under the command box.

Label the buttons on the left 'Connect to Database' (ConnectButton)and 'Exit' (ExitButton) and the one on the right 'Send Query' (QueryButton).

Place a large Memo Box labelled and named 'Results' (ResultMemo) on the lower right, to fill most of the available space. Find its ScrollBars property and select ssAutoBoth so that scroll bars appear automatically if text fills the space. Make the WordWrap property True.

Place a Status Bar (from the Common Controls tab) at the bottom of the Form, and make its SimpleText property 'TryMySQL'.

A screenshot of the Form can be seen here: Mysql Example Screenshot

Now we need to write some event handlers.

The three Edit boxes on the left are for entry of hostname, username and password. When these have been entered satisfactorily, the Connect Button is clicked. The OnCLick event handler for this button is based on part of the text-mode FPC program above.

The responses from the database should be converted into strings and displayed in the Memo box. Text mode Pascal write and writeln statements are capable of performing a lot of type conversion 'on the fly', but the use of a memo box for text output requires explicit conversion of data types to the correct string: so Pchar variables have to be converted to strings using StrPas, and integers have to be converted with IntToStr.

Strings are displayed in the Memo box using:

procedure ShowString(S : string);
(* display a string in a Memo box *)
begin
  trymysqlForm1.ResultsMemo.Lines.Add(S)
end;

The ConnectButton event handler thus becomes:

procedure TtrymysqlForm1.ConnectButtonClick(Sender: TObject);
(* Connect to MySQL using user data from Text entry boxes on Main Form *)
var strg: string;
  
begin
  dummy1 :=  trymysqlForm1.HostEdit.text+#0;
  host := @dummy1[1];
  dummy2 := trymysqlForm1.UserEdit.text+#0;
  user := @dummy2[1] ;
  dummy3 := trymysqlForm1.PasswdEdit.text+#0;
  passwd := @dummy3[1] ;

  alloc := mysql_init(PMYSQL(@qmysql));
  sock :=  mysql_real_connect(alloc, host, user, passwd, database, 0, nil, 0);
  if sock=Nil then
    begin
      strg :='Couldn''t connect to MySQL.'; showstring (strg);
      Strg :='Error was: '+ StrPas(mysql_error(@qmysql)); showstring (strg);
    end
    else
    begin
      trymysqlForm1.statusBar1.simpletext := 'Connected to MySQL';
      strg := 'Now choosing database : ' + database; showstring (strg);
 {$ifdef Unix}
      strg :='Mysql_port      : '+ IntToStr(mysql_port); showstring (strg);
      strg :='Mysql_unix_port : ' + StrPas(mysql_unix_port); showstring (strg);
 {$endif}
      Strg :='Host info       : ' + StrPas(mysql_get_host_info(sock));
      showstring (strg);
      Strg :='Server info     : ' + StrPas(mysql_stat(sock)); showstring (strg);
      Strg :='Client info     : ' + Strpas(mysql_get_client_info);  showstring (strg);
  
      trymysqlForm1.statusbar1.simpletext := 'Selecting Database ' + DataBase +'...';
      if mysql_select_db(sock,DataBase) < 0 then
      begin
        strg :='Couldn''t select database '+ Database; ShowString (strg);
        Strg := mysql_error(sock); ShowString (strg);
      end
    end;
end;

The Text Box on the right allows entry of a SQL statement, without a terminal semicolon; when you are satisfied with its content or syntax, the SendQuery button is pressed, and the query is processed, with results being written in the ResultsMemo box.

The SendQuery event handler is again based on the FPC text-mode version, except that once again explicit type-conversion has to be done before strings are displayed in the box.

A difference from the text-mode FPC program is that if an error condition is detected, the program does not halt and MySQL is not closed; instead, control is returned to the main form and an opportunity is given to correct the entry before the command is re-submitted. The application finally exits (with closure of MySQL) when the Exit Button is clicked.

The code for SendQuery follows:

procedure TtrymysqlForm1.QueryButtonClick(Sender: TObject);
var
  dumquery, strg: string;
begin
  dumquery := TrymysqlForm1.CommandEdit.text;
  dumquery := dumquery+#0;
  query := @dumquery[1];
  trymysqlForm1.statusbar1.simpletext := 'Executing query : '+ dumQuery +'...';
  strg := 'Executing query : ' + dumQuery; showstring (strg);
  if (mysql_query(sock,Query) < 0) then
  begin
    Strg :='Query failed '+ StrPas(mysql_error(sock)); showstring (strg);
  end
  else
  begin
    recbuf := mysql_store_result(sock);
    if RecBuf=Nil then
    begin
      Strg :='Query returned nil result.'; showstring (strg);
    end
    else
    begin
      strg :='Number of records returned  : ' + IntToStr(mysql_num_rows (recbuf));
      Showstring (strg);
      Strg :='Number of fields per record : ' + IntToStr(mysql_num_fields(recbuf));
      showstring (strg);
      rowbuf := mysql_fetch_row(recbuf);
      while (rowbuf <>nil) do
      begin
        Strg :='(Id: '+ rowbuf[0]+', Name: ' + rowbuf[1]+ ', Email : ' +
        rowbuf[2] +')';
        showstring(strg);
        rowbuf := mysql_fetch_row(recbuf);
      end;
    end;
  end;
end;

Save your Project, and press Run -> Run

Télécharger le code source de l'exemple

Warning-icon.png

Avertissement: Current versions of Lazarus/FPC require committing all MySQL transactions. This was not necessary in earlier versions. The code download likely will not work until it is updated.

A full listing of the program is available here Sample Source Code

Lazarus, MySQL et UTF-8

The following may be required for other codepages/character sets as well

UTF-8 Unicode is a convenient multibyte character set encoding, that allows working with multilingual texts without requiring WideStrings. It is supported both by Lazarus SQLdb components and by MySQL since version 4.1 by choosing the appropriate character set.

However, simply setting this encoding as default for

  • your tables and
  • the MySQL connection component (e.g. TMySQL51Connection.CharSet:='UTF8';)

will result in incorrect storage and retrieval of UTF-8 strings: any accented/international character will show up as question mark (?). Apparently, the reason for this is that MySQL client library is compiled to expect Latin1 character set by default.

In order to enable proper communication between Lazarus, MySQL client library and MySQL server, additional two queries need to be executed each time a connection to the database is established:

SET CHARACTER SET `utf8`

and

SET NAMES 'utf8'

The first query will ensure your application receives strings in correct encoding, and the second tells MySQL not to convert strings it receives from your application.

Simple démo MySQL utilisant le composant TMySQL5xConnection

Here is code that functions as a quick demo to get up and running simply. As with all SQLDB components, make sure the database client library is in the correct place:

  • on Windows: the DLL, e.g. libmysql.dll is put in the project output directory (where the executable is generated). Alternatively, you could also place it in your Windows/system32 directory
  • on Linux/OSX: install the mysql client library in your path (e.g. using your distribution's package manager)

When distributing your application, make sure the proper MySQL client library is present on your user's computer. As this can be a problem, perhaps using a different database engine may make more sense.

Exemple conduit par le code

The example below uses code to fill your controls with data. You can also use data-bound controls, which might be quicker/easier. See the example below it or SQLdb_Tutorial1 for this.

Place three edit boxes, a memo box and a few buttons on the form. You need to add mysqlXXconn and sqldb to the uses statement.

In this example, the MySQL DBMS has a user 'root' with no password, and a database 'test1' with table 'tPerson' which has three fields: 'personid' (int), 'surname' (varchar(40)) and 'dob' (datetime). Also, some test data was inserted.

The button btnTest must be clicked first as it creates the connection with the DBMS. Note the line that applies updates - without this the changed or new data will not be written back to the DB though they will be in memory and can be viewed using btnFirst and btnNext.

unit unt_db;
// Example based on:
// http://www.lazarus.freepascal.org/index.php?name=PNphpBB2&file=viewtopic&t=5761
// from tpglemur on that forum
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
  mysql50conn, sqldb, StdCtrls;
type
  { TForm1 }
  TForm1 = class(TForm)
    btnTest: TButton;
    btnNext: TButton;
    btnFirst: TButton;
    btnNew: TButton;
    edtPersonID: TEdit;
    edtSurname: TEdit;
    edtDOB: TEdit;
    Memo1: TMemo;
    procedure btnFirstClick(Sender: TObject);
    procedure btnNewClick(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure btnTestClick(Sender: TObject);
  private
    { private declarations }
    conn : TMySQL50Connection;
    query : TSQLQuery;
    transaction : TSQLTransaction;
    procedure Display;
  public
    { public declarations }
  end;
var
  Form1: TForm1;
implementation
{ TForm1 }
procedure TForm1.btnTestClick(Sender: TObject);
var
  S: String;
begin
  conn := TMySQL50Connection.Create(nil);
  query := TSQLQuery.Create(nil);
  transaction := TSQLTransaction.Create(nil);
  try
    try
      // Adjust to your own database server, username and password:
      conn.HostName := '127.0.0.1';
      conn.UserName := 'root';
      conn.Password := '';
      // If you use a different database name, adjust here:
      conn.DatabaseName := 'test1';
      conn.Connected := True;
      conn.Transaction := transaction;
      query.DataBase := conn;
      //query.ParseSQL := true; //line not needed - this is the default anyway
      //query.ReadOnly := false; //line not needed - this is the default anyway
      query.SQL.Text := 'select * from tperson';
      query.Open;

      query.Last;
      S := IntToStr(query.RecordCount) + #13#10;
      query.First;

      while not query.EOF do
      begin
        S := S + query.FieldByName('surname').AsString + #13#10;
        query.Next;
      end;
    finally
      //query.Free;
      //conn.Free;
    end;
  except
    on E: Exception do
      ShowMessage(E.message);
  end;
  Memo1.Text:= S;
end;

procedure TForm1.Display;
begin
  edtPersonID.Text := query.FieldByName('personid').AsString;
  edtSurname.Text := query.FieldByName('surname').AsString;
  edtDOB.Text := query.FieldByName('dob').AsString;
end;

procedure TForm1.btnFirstClick(Sender: TObject);
begin
  query.First;
  Display;
end;

procedure TForm1.btnNewClick(Sender: TObject);
begin
  query.Append;
  query.FieldValues['personid'] := edtPersonID.Text;
  query.FieldValues['surname'] := edtSurname.Text;
  query.FieldValues['dob'] := edtDOB.Text;
  query.Post;  
  query.ApplyUpdates; //to apply update
  transaction.Commit; //Needed since FPC 2.6.4; todo: somebody should check if this is enough
end;

procedure TForm1.btnNextClick(Sender: TObject);
begin
  query.Next;
  Display;
end;

initialization
  {$I unt_db.lrs}
end.

RAD/Contrôles liés aux données

Veuillez voir Tutoriel SQLdb 1, Tutoriel SQLdb 2 et plus de tutoriels.

Zeos

Voir ZeosDBO

Pascal Data Objects (PDO)

Pascal Data Objects is an alternative data access layer that seems to support:

  • Clients MySQL 4.0
  • Clients MySQL 4.1
  • Clients MySQL 5.0
  • (et aussi) Firebird 1.5 et 2.0

Les fonctions introduites dans MySQL 4.1 et 5.0 comme les instructions préparées, la liaison et les procéduresstockées sont prises en charge. PDO est inspiré de PHP Data Objects. Tout le code et la documentation nécessaire à l'emploi de cette API est disponible sur sourceforge.

Paquet MySQL : Les unités de bas-niveau

As with all databases, the SQLDB code depends on a lower level mysql specific unit that wraps around the mysql driver library (.so/.dll). Normally, you would use the higher-level SQLDB code as it allows you to code more quickly, easily switch databases etc.

Using this is very easy, all you need to do is compile some units, and use these units in your program. You need to specify the location in the filesystem of the MySQL client Library (libmysqlclient on Linux) when compiling, and that is it.

Unités et programmes fournis

The packages provides 3 units, of which normally only the first is needed:

  • mysql the main mysql unit.
  • mysql<version> (e.g. mysql50) provides access to the specific mysql library for that version. Note: the client library version is unrelated to the version of the server in use - except that you want to make sure these versions are compatible.
  • mysql<version>com contains some internal routines of MySQL (presumably a translation of mysql_com.h); it should normally not be used unless you want access to some internal types.

Example programs can be found in the <fpc>\packages\mysql\examples directory.

Installation

The mysql interface is distributed with the Free Pascal packages, and come with the compiler distribution: Normally no action should be taken to work with MySQL.

In case you want to modify and compile the units yourself, the mysql sources are in the packages directory: packages/mysql

This directory contains the units, a test program and a makefile. cd to the directory and type

make

This should compile the units. If compilation was succesful, you can install with

make install

You can then test the program by running

make test

This will:

  • Run a script to create a table in a database, and fill it with some data. (the mysql program should be in your PATH for this) . By default, the used database is testdb.
  • Run the testprogram testdb
  • Run a shell script again to remove the created table.

You will see a lot of messages on your screen, giving you feedback and results. If something went wrong, make will inform you of this.

Go back to Packages List

Voir aussi