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

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

然后在测试连接中,写入以下代码(注意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;

posted on 2018-09-14 09:27 liuweijie 阅读(...) 评论(...) 编辑 收藏

公告

统计

  • 随笔 - 24
  • 文章 - 0
  • 评论 - 0