MySQL MySql和Delphi直连
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8369597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySql and Delphi direct connection
提问by opc0de
I am looking into a free solution to connect delphi with a mysql database but without using ODBC.Is there such a component ?
我正在寻找一个免费的解决方案,将 delphi 与 mysql 数据库连接,但不使用 ODBC。有这样的组件吗?
Thanks.
谢谢。
回答by Pateman
回答by Khaled Ahmed
i have been looking and using for years many tools, free and paid.
多年来,我一直在寻找和使用许多免费和付费的工具。
1st free is weak or difficult, at least u need too much code to do simple tasks which can be done in one or two functions in paid tools.
第一次免费很弱或很难,至少你需要太多的代码来完成一些简单的任务,而这些任务可以通过付费工具中的一两个功能来完成。
Paid tools i have used Devart MyDAC and microOLAP for MySQL which are the top rated tools in this area. i used mydac for around 2 years, but lately i moved to MicroOLAP DAC for MySQL as a better alternative for many reasons
付费工具我使用了 Devart MyDAC 和 microOLAP for MySQL,它们是该领域评价最高的工具。我使用 mydac 大约 2 年,但最近我转向 MicroOLAP DAC for MySQL 作为更好的选择,原因有很多
- microlap mydac is much smaller, much easier to maintain and install and to use, mydac is much bigger details and many of these details and properties give unclear errors when used in some wrong way. since microolap is smaller, it is almost error free, and easier to use.
- mydac has a unicode issue in TmyDump vcl component, which repeatly appear and fixed in next update and reappear in the next one, fixed in the next and so on. this bug creats a nonunicode backup file containing unicode data, which when restore with the fixed version will cause wrong unicode conversion and damage of data, MicroOLAP tool has no issue with that with its similar component and smaller amount of details.
- i believe simplicity is much better than complications, easier, and much bug free. that is main reasons for me to convert to DAC for MySQL from MicroOLAP.
- microlap mydac 更小,更易于维护、安装和使用,mydac 的细节要大得多,当以错误的方式使用时,这些细节和属性中的许多会产生不清楚的错误。由于 microolap 更小,它几乎没有错误,并且更易于使用。
- mydac 在 TmyDump vcl 组件中存在 unicode 问题,该问题在下一次更新中重复出现并修复,并在下一次再次出现,在下一次中修复,依此类推。这个bug会创建一个包含unicode数据的非unicode备份文件,当用固定版本恢复时会导致unicode转换错误和数据损坏,MicroOLAP工具没有问题,组件相似,细节较少。
- 我相信简单比复杂好得多,更容易,而且没有很多错误。这是我从 MicroOLAP 转换为 MySQL 的 DAC 的主要原因。
that is my simple experiement with this important basic subject for any serious database busniness developer.
对于任何认真的数据库业务开发人员来说,这是我对这个重要基本主题的简单实验。
i tried also the new FireDAC, i highly discourage to use it, too much complications, difficult unicode handling, difficult to deploy.
我也尝试过新的 FireDAC,我非常不鼓励使用它,太多的复杂性,难以处理 unicode,难以部署。
Have luck.
祝你好运。
回答by Torbins
You can use dbExpress. The only thing you will need is libmysql.dll from 5.1.X server.
您可以使用 dbExpress。您唯一需要的是来自 5.1.X 服务器的 libmysql.dll。
回答by Mohammed Nasman
I have been using MyDac since years, which is one of best DAC components for Delphi.
我多年来一直在使用 MyDac,它是 Delphi 最好的 DAC 组件之一。
AFAIK it's the only native component that offer Direct Connection to MySql (No ODBC, No OLEDB, No libmysql.dll).
AFAIK 它是唯一提供与 MySql 直接连接的本机组件(无 ODBC、无 OLEDB、无 libmysql.dll)。
回答by Edijs Kolesnikovi?s
This is what I use in Delphi XE4 (works in previous versions too). It creates components during runtime. Note: if you want to create databases, the default MySQL database 'mysql' needs to be used. Make sure you check if you have access to it, put try..except..end; in your code. And yes, it requires having dbxmys.dll and libmysql.dll in the same folder as your *.exe. This video might give you some hints http://www.youtube.com/watch?v=6mRGAB4LsEE
这就是我在 Delphi XE4 中使用的(也适用于以前的版本)。它在运行时创建组件。注意:如果要创建数据库,需要使用默认的 MySQL 数据库 'mysql'。确保您检查是否可以访问它,将 try..except..end; 在你的代码中。是的,它需要将 dbxmys.dll 和 libmysql.dll 放在与 *.exe 相同的文件夹中。这个视频可能会给你一些提示http://www.youtube.com/watch?v=6mRGAB4LsEE
unit MainUnit;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms,
FMX.StdCtrls, Data.DBXMySQL, FMX.Edit, Data.DB, Data.SqlExpr, FMX.Dialogs, Windows,
Data.FMTBcd, FMX.Layouts, FMX.Memo, FMX.ListBox, FMX.ListView.Types,
FMX.ListView;
type
TForm3 = class(TForm)
Button1: TButton;
Label1: TLabel;
Edit1: TEdit;
Memo1: TMemo;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MySQLConnection: TSQLConnection;
MySQLQuery: TSQLQuery;
Function ConnectToMySQLDatabase(szHostName, szUserName, szPassword, szDatabaseName: String): boolean;
end;
var
Form3: TForm3;
implementation
{$R *.fmx}
procedure TForm3.Button1Click(Sender: TObject);
begin
if ConnectToMySQLDatabase('localhost', 'root', 'passw_ord', 'table_name') = False then
Caption := 'Not Connected'
else
begin
Caption := 'Connected';
try
MySQLQuery.SQL.Clear;
{MySQLQuery.SQL.Add('insert into table_name(vardas_pavarde, asmens_kodas, kodas, pazym_nr, registravimo_data, '+
'data_nuo_kada_taikomas, isregistravimo_data, negalioja_nuo, paskelbimas_negaliojanciu, priezastis, pastabos) '+
'values ("Edijs Test", "3001000", "38", "PazPK122", "2013.05.03", "2013.06.01", NULL, NULL, NULL, "Tu??ia", '+
'"????????ū?");');}
MySQLQuery.SQL.Add('select * from table_name where vardas_pavarde="edIJS tEst";');
MySQLQuery.Open;
Memo1.Lines.Add(VarToSTr(MySQLQuery['vardas_pavarde']));
Memo1.Lines.Add(VarToSTr(MySQLQuery['asmens_kodas']));
Memo1.Lines.Add(VarToSTr(MySQLQuery['pastabos']));
MySQLQuery.Close;
except
on E: Exception do
MessageBox(0, PWideChar(E.Message), 'Error', MB_ICONERROR);
end;
end;
end;
Function TForm3.ConnectToMySQLDatabase(szHostName, szUserName, szPassword, szDatabaseName: String): boolean;
begin
MySQLConnection := FindComponent('MySQLConnection') as TSQLConnection;
if not Assigned(MySQLConnection) then
MySQLConnection := TSQLConnection.Create(Self);
MySQLConnection.DriverName := 'MySQL';
MySQLConnection.GetDriverFunc := 'getSQLDriverMYSQL';
MySQLConnection.LibraryName := 'dbxmys.dll';
MySQLConnection.VendorLib := 'LIBMYSQL.dll';
MySQLConnection.Params.Values['HostName'] := szHostName;
MySQLConnection.Params.Values['Database'] := szDatabaseName;
MySQLConnection.Params.Values['User_Name'] := szUserName;
MySQLConnection.Params.Values['Password'] := szPassword;
MySQLConnection.Params.Values['ServerCharSet'] := 'utf8';
MySQLConnection.LoginPrompt := False;
try
MySQLConnection.Connected := True;
MySQLQuery := FindComponent('MySQLQuery') as TSQLQuery;
if not Assigned(MySQLQuery) then
MySQLQuery := TSQLQuery.Create(Self);
MySQLQuery.SQLConnection := MySQLConnection;
Result := True;
except
on E: Exception do
begina
MessageBox(0, PWideChar(E.Message), 'Error', MB_ICONERROR);
Result := False;
end;
end;
end;
end.
回答by ssh
Use DB Express with MySQL 5.x in Delphi 7 See this link
在 Delphi 7 中将 DB Express 与 MySQL 5.x 一起使用 请参阅此链接
http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html
http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html