delphi7连接mysql5的实现方法
本文简单介绍了Delphi7连接MySQL数据库的实现方法,具体步骤如下:
首先先去下载:http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
然后将下载到的dbxopenmysql5_dll.zip解压出来,再把dbxopenmysql50.dll和libmysql.dll都放到工程文件夹下。
在Form上放上TSQLConnection、TSQLQuery、TStringGrid、3个TButton、TLable。
添加如下代码:
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,DBXpress,FMTBcd,StdCtrls,Grids,DB,SqlExpr;
type
TForm1=class(TForm)
SQLConnection1:TSQLConnection;
SQLQuery1:TSQLQuery;
StringGrid1:TStringGrid;
Button1:TButton;
Button2:TButton;
Button3:TButton;
Label1:TLabel;
procedureButton1Click(Sender:TObject);
procedureButton2Click(Sender:TObject);
procedureButton3Click(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
{$R*.dfm}
procedureTForm1.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;
ifSQLConnection1.Connected=truethen
begin
SQLQuery1.SQLConnection:=SQLConnection1;
Label1.Caption:='success!';
end
else
Label1.Caption:='failed!';
end;
procedureTForm1.Button2Click(Sender:TObject);
var
i,j:Integer;
begin
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('SELECT*FROMuser');
SQLQuery1.Active:=true;
i:=0;
SQLQuery1.First;
whilenotSQLQuery1.eofdo
begin
forj:=0toSQLQuery1.FieldCount-1do
StringGrid1.cells[j,i]:=SQLQuery1.Fields[j].AsString;
SQLQuery1.next;
inc(i);
end;
SQLQuery1.Active:=false;
end;
procedureTForm1.Button3Click(Sender:TObject);
begin
ifSQLConnection1.Connected=truethen
SQLConnection1.Close;
SQLConnection1.Free;
end;
end.
经测试,可实现正常连接与查询。