欢迎来到代码驿站!

Delphi代码

当前位置:首页 > 软件编程 > Delphi代码

delphi7连接mysql5的实现方法

时间:2021-04-03 07:45:12|栏目:Delphi代码|点击:

本文简单介绍了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。

添加如下代码:

unit Unit1;
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;
  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 not SQLQuery1.eof do
 begin
  for j := 0 to SQLQuery1.FieldCount - 1 do
   StringGrid1.cells[j, i] := SQLQuery1.Fields[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.
  

经测试,可实现正常连接与查询。

上一篇:Delphi隐藏TPageControl的标签实例介绍

栏    目:Delphi代码

下一篇:Delphi实现检测并枚举系统安装的打印机的方法

本文标题:delphi7连接mysql5的实现方法

本文地址:http://www.codeinn.net/misctech/93299.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有