Cet article présente brièvement la méthode d'implémentation de Delphi7 se connectant à la base de données MySQL. Les étapes spécifiques sont les suivantes :
Téléchargez-le d'abord : http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
Décompressez ensuite le fichier dbxopenmysql5_dll.zip téléchargé et placez dbxopenmysql50.dll et libmysql.dll dans le dossier du projet.
Placez TSQLConnection, TSQLQuery, TStringGrid, 3 TButtons et TLable sur le formulaire.
Ajoutez le code suivant :
unité Unit1 ; interface utilise Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DBXpress, FMTBcd, StdCtrls, Grids, DB, SqlExpr ; type TForm1 = class(TForm) SQLConnection1 : TSQLConnection : TSQLQuery ; : TStringGrid Bouton1 : TButton; Button2: TButton; TButton; Label1: TLabel; procédure Button1Click (Expéditeur: TObject); procédure Button3Click (Expéditeur: TObject); var Form1 : TForm1 ; implémentation de la procédure {$R *.dfm} TForm1.Button1Click(Expéditeur : TObject);begin SQLConnection1 := TSQLConnection.Create(nil); SQLConnection1.DriverName := 'dbxmysql'; SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50' SQLConnection1.LibraryName := 'dbxopenmysql50.dll'; SQLConnection1.VendorLib := 'libmysql.dll'; SQLConnection1.LoginPrompt := false; SQLConnection1.Params.Append('Database=mysql'); SQLConnection1.Params.Append('User_Name=root'); 'Mot de passe='); SQLConnection1.Params.Append('HostName=localhost'); SQLConnection1.Open; si SQLConnection1.Connected = true alors commencez SQLQuery1.SQLConnection := SQLConnection1; échoué !';fin; procédure TForm1.Button2Click(Expéditeur : TObject);var i, j : Integer;begin SQLQuery1.SQL.Clear; SQLQuery1.SQL.Add('SELECT * FROM user'); SQLQuery1.Active := true; SQLQuery1.First alors que SQLQuery1.eof ne commence pas pour j := 0 à SQLQuery1; .FieldCount - 1 faire StringGrid1.cells[j, i] := SQLQuery1.Fields[j].AsString; SQLQuery1.next; inc(i); end; SQLQuery1.Active := false;end; procédure TForm1.Button3Click(Sender: TObject);begin si SQLConnection1.Connected = true alors SQLConnection1.Free;end;
Après le test, une connexion et une requête normales peuvent être obtenues.