Delphi FireDAC connect to MySQL database

RAD Studio 10.3 test √

Required controls : FDConnection, FDPhysMySQLDriverLink, FDQuery, DataSource

Note : The connection is MySQL, the other difference should not be very big (just a guess), just simply connect to the database.

Steps :
1. Create a new DataModule [similar to a form]
DataModule (data module)
2. Find the protagonist TFDConnection control in the FireDAC tab, and place it on the DataModule form, and set LoginPrompt to False, so that it will not pop up when connected Log in to the dialog box, you can also choose to give up this step. [Prompt that the DLL cannot be found, then set the Connected property after setting the following properties]

Method 1 : Set directly on the control without writing code.
TFDConnection
Method 2 : After dragging the control, do not do any operation, directly upload the code [execute before adjusting the database, the code written later is the same, you must use it before. Otherwise, you will be lonely)

  FDConnection1.Params.DriverID := 'MySQL';
  FDConnection1.Params.Add('Server=IP地址');
  FDConnection1.Params.Add('Port=3306');//默认端口可省略这行
  FDConnection1.Params.Database := '数据库名';
  FDConnection1.Params.UserName := '数据库账号';
  FDConnection1.Params.Password := '数据库密码';
  FDConnection1.Params.Add('CharacterSet=utf8');
  try
    FDConnection1.Open();//和下面一句是同样的效果
//    FDConnection1.Connected := True;
  except on E: Exception do
    ShowMessage(e.Message);
    // ShowMessage 编译报错的话看看有没有声明 【uses Vcl.Dialogs;】
  end;

3. FDPhysMySQLDriverLink control, I only know that it is the stuff of the dynamic library associated with MySQL, then it is time to get the libmysql.dll file, it would be better if you have it yourself.
Link: libmysql.dll download address
Extraction code: c3jc [Can’t tell me, I’ll change it in time, if I really need it, I can find one at some point]

Method one : also directly operate on the control
FDPhysMySQLDriverLink
method two : directly upload the code [this code is placed before the above code]

  FDPhysMySQLDriverLink1.VendorLib := 'libmysql.dll的地址';

[You can also now run the .exe program put together]

4. Start working on the FDQuery control, the same formula
method 1 : Directly working on the control [Active is set last, the value of the control just put on is False]

Attributes Attribute value
Active True
Connection FDConnection1
SQL Click to write SQL statement

Method 2 : Still the same whole process code, just drop the control and don’t care

  FDQuery1.Connection := FDConnection1;
  FDQuery1.SQL.Add('SQL语句');
  FDQuery1.Active := True;

5. Success is at hand, and we are done with the DataSource control.
Method 1 : Make the control.
DataSource
Method 2 : Still the same whole code, leave the control alone. [The suggestion is that the method is convenient and quick]

  DataSource1.DataSet := FDQuery1;

A little bit of notes recorded during study, so that you can read it later.

Guess you like

Origin blog.csdn.net/qq_44111597/article/details/108256831