delphi连接mysql (通过libmysql.dll连接)

首先在窗体上拖拽sqlconnection和sqlquery两个控件:

delphi连接mysql (通过libmysql.dll连接)

delphi连接mysql (通过libmysql.dll连接)

然后在测试连接中,写入以下代码(注意exe生成目录下需要有dbxopenmysql50.dll和libmysql.dll

  1. SQLConnection1 := TSQLConnection.Create(nil);
  2. SQLConnection1.DriverName := 'dbxmysql';
  3. SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50';
  4. SQLConnection1.LibraryName := 'dbxopenmysql50.dll';
  5. SQLConnection1.VendorLib := 'libmysql.dll';
  6. SQLConnection1.LoginPrompt := false;
  7. SQLConnection1.Params.Append('Database=sampledb31');
  8. SQLConnection1.Params.Append('User_Name=test');
  9. SQLConnection1.Params.Append('Password=test');
  10. SQLConnection1.Params.Append('HostName=192.168.1.78');
  11.  
  12. SQLConnection1.Open;
  13. if SQLConnection1.Connected = true then
  14. begin
  15. SQLQuery1.SQLConnection := SQLConnection1;
  16. SQLQuery1.SQL.Clear;
  17. SQLQuery1.SQL.Text := 'SET NAMES gbk;';//设置mysql查询中文不乱码
  18. SQLQuery1.ExecSQL();
  19. MessageBox(0,'连接成功','提示',MB_ICONASTERISK and MB_ICONINFORMATION);
  20. end else
  21. begin
  22. showmessage('数据库连接失败!');
  23. SQLConnection1.Close;
  24. end;
上一篇:为什么MVC不是一种设计模式(转)


下一篇:Linux/UNIX之信号(2)