Delphi2006 DBExpress 连接 Mysql5.1

DBExpress+dbxopenmysql50.dll

由于Delphi2006中的DBExpress对Mysql高版本的支持很差,从国外论坛上看到的说法似乎是根本就没实现,所以说虽然TSQLConnection组件中提供了Mysql选项,但直接使用的话是不行的(低版本的mysql可能可以),我遇到的现象是提示“Unable to Load libmysql.dll”,但其实我已经在系统目录System32下、Delphi安装目录的bin中、开发工程项目文件夹中都安放了该文件,还是找不到该dll。
dbxopenmysql50.dll是由老外开发的,而且开源,还是老外好啊,可以到如下网址去下载:
http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
使用时需要将dbxopenmysql50.dll和libmysql.dll都放到工程文件夹下。顺便提一下,libmysql.dll在mysql安装目录下的libopt目录中。
使用方法有两种,一种是直接修改BorlandBDS4.0dbExpress下的dbxdrivers.ini,调整其中关于mysql的各参数。
另一种就是在程序中指定,现在我以这种方式为例说明这种连接方式的开发方法。
在Form上放上TSQLConnection、TSQLQuery、TStringGrid、3个TButton、TLable。

在FormCreate事件中:
SQLConnection1 := TSQLConnection.Create(nil);
SQLConnection1.DriverName := 'dbxmysql';
SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50';
SQLConnection1.LibraryName := 'dbxopenmysql50.dll';
SQLConnection1.VendorLib := 'libmysql.dll';
SQLConnection1.LoginPrompt := false;
在此设置TSQLConnection的各个具体参数,当然也可以直接在组件属性面板中修改,或者修改dbxdrivers.ini中的对应参数,方法是多种的。
Connect按钮的事件:
SQLConnection1.Params.Append('Database=user');
SQLConnection1.Params.Append('User_Name=mysql');
SQLConnection1.Params.Append('Password=mysql');
SQLConnection1.Params.Append('HostName=localhost');
SQLConnection1.Open;
if SQLConnection1.Connected = true then
Label1.Caption := 'success'
else
Label1.Caption := 'fail';
设置数据库连接的各参数配置后,打开数据库连接,同时显示连接是否成功。
Query按钮的事件:
var
i,j: Integer;
begin
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('SELECT * FROM userinfo');
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;
查询表数据并在StringGrid中输出。
Disconnect按钮的事件:
if SQLConnection1.Connected = true then
SQLConnection1.Close;
FormDestroy事件:
if SQLConnection1.Connected = true then
SQLConnection1.Close;
SQLConnection1.Free;
运行程序,点击connect按钮,如果数据库连接成功可以看到success提示,然后点击query按钮就能查询到表中的数据。

下载(dbxopenmysql50.dll)
点击下载
下载(dbxopenmysql50.dll源码)
点击下载

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注