This paper briefly introduces the implementation method of DELPHI7 connection MySQL database, the following steps are as follows:
First go to download: http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
Then the download to the Dbxopenmysql5_dll.zip to extract, and then Dbxopenmysql50.dll and Libmysql.dll are placed under the project folder.
On the form, put Tsqlconnection, Tsqlquery, Tstringgrid, 3 TButton, tlable.
Add the following code:
Unit Unit1; Interface uses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, dbxpress, FMTBCD, STD
Ctrls, Grids, DB, sqlexpr;
Type TFORM1 = Class (Tform) sqlconnection1:tsqlconnection;
Sqlquery1:tsqlquery;
Stringgrid1:tstringgrid;
Button1:tbutton;
Button2:tbutton;
Button3:tbutton;
Label1:tlabel;
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Private {Private declarations} public {public declarations} end;
var Form1:tform1;
Implementation {$R *.DFM} procedure Tform1.button1click (Sender: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 ');
SQLConnection1.Params.Append (' password= ');
SQLConnection1.Params.Append (' Hostname=localhost ');
Sqlconnection1.open;
If sqlconnection1.connected = True THEN BEGIN sqlquery1.sqlconnection: = SQLConnection1;
Label1.Caption: = ' success! ';
End else Label1.Caption: = ' failed! ';
End
Procedure Tform1.button2click (Sender:tobject);
var i, J:integer;
Begin SQLQuery1.SQL.Clear;
SQLQUERY1.SQL.ADD (' SELECT * from user ');
Sqlquery1.active: = true;
I: = 0;
Sqlquery1.first; While does sqlquery1.eof do begin with J: = 0 to Sqlquery1.fieldcount-1 do stringgrid1.cells[j, I]: = Sqlquery1.fiel DS[J].
asstring;
Sqlquery1.next;
Inc (I);
End
Sqlquery1.active: = false;
End
Procedure Tform1.button3click (Sender:tobject);
Begin IF sqlconnection1.connected = True Then Sqlconnection1.close;
Sqlconnection1.free;
End
End.
The
is tested to enable normal connections and queries.