MySql and Delphi direct connection

前端 未结 6 582
慢半拍i
慢半拍i 2021-01-25 04:47

I am looking into a free solution to connect delphi with a mysql database but without using ODBC.Is there such a component ?

Thanks.

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 05:40

    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

    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.
    

提交回复
热议问题