用户名:  密码:   
您当前的位置:首页 > SQL数据库 >  MySql > 

delphi7中使用dbExpress方式连接mysql数据库的方法

上次修改时间: 2024-06-07 17:53:33      来源:网络总结      作者:钢铁长陈
上一页      路径:DSD\Learn\SQL数据库相关\mysql相关\delphi7中使用dbExpress方式连接mysql数据库\      下一页

delphi7中使用dbExpress方式连接数据库的方法

参考地址:
https://www.cnblogs.com/smartlittleant/p/4064483.html
https://www.jb51.net/article/52267.htm

注:不建议使用dbExpress,官网dbExpress出来的都是迁移到FireDAC的方法,下面方法在delphi7下测试通过,delphi11下不能用
注2: dbExpress下一些控件如sqlquery,sqldataset,sqktable游标是单向的,而dbgrid游标是双向的,不能使用ADO方式的con+sqlquery+DataSource1+DBgrid方式,否则提示"Operation not allowed on unidirectional dataset"错误。 只能用SQLConnection1+SimpleDateSet1+DataSource1+DBGrid1
或者SQLQuery+DataSetProvider+ClientDataSet+DataSource +DBGrid,这种方式未测试

一、使用SQLConnection1+SimpleDateSet1+DataSource1+DBGrid1方式连接数据库
1. 需要两个dll文件,一个是libmysql.dll,和dbxopenmysql50.dll,
libmysql同fireDAC使用的是一样的
dbxopenmysql50.dll可到https://www.justsoftwaresolutions.co.uk/downloads.html, 下载,只支持mysql5.0,不支持最新到

2. 将上面两个程序复制到工程目录下,
 1)设置SQLConnection1的属性,分别设置ConnectonName,DriverName为MySQLConnection,MySQL
 GetDriverFunc,LibrayName要改为,getSQLDriverMYSQL50和dbxopenmysql50.dll //这个dll下载地址见第一步
 再将LoginPrompt设为false,connected设为true
 2)在Params中设置Drivername,Hostname,Database,User_Name,Password分别为Mysql,192.168.13.77,ctcms,root,root
  注:若Params中为空,表1)中的连接名和驱动名没有选择
3. 将SimpleDataSet1的Connection设为Connection1,ComandText设为Select * from ct_link

4. 其它DataSource1与DBGrid同ADO方式一样


二、 代码方式连接,dll先复制到工程目录
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=ctcms');
 SQLConnection1.Params.Append('User_Name=root');
 SQLConnection1.Params.Append('Password=root');
 SQLConnection1.Params.Append('HostName=192.168.13.77');
 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 ct_linktype');
 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;