Einzelnen Beitrag anzeigen

Benutzerbild von Deltachaos
Deltachaos

Registriert seit: 20. Feb 2008
Ort: Berlin
195 Beiträge
 
Delphi 7 Personal
 
#1

per MySQL direct komplette Datenbank lesen und speichern?

  Alt 2. Mai 2008, 20:33
Datenbank: MySQL • Zugriff über: MySQL direct (Delphi)
Hallo kan ich per MySQL Direct die komplette Datenbank exportiren/denn inhalt igendwie Speichern?

Ich kann schon eine Verbindung aufbauen.

Hier mein Codeansatz:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, StdCtrls, ComCtrls, inifiles, umysqlvio, uMysqlCT, uMysqlClient, uMysqlHelpers;

{$I mysqlinc.inc}

type
  TForm1 = class(TForm)
    IdFTP1: TIdFTP;
    TrackBar1: TTrackBar;
    Button1: TButton;
    Memo1: TMemo;
    hostname: TEdit;
    port: TEdit;
    username: TEdit;
    password: TEdit;
    Button2: TButton;
    Button3: TButton;
    mysqlhost: TEdit;
    mysqlport: TEdit;
    mysqlusername: TEdit;
    mysqlpassword: TEdit;
    mysqldatenbankname: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FMysql: TMysqlClient;
    FResult: TMysqlResult;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var log, sett: TIniFile;
    Present: TDateTime;
    Year, Month, Day, Hour, Min, Sec, MSec: Word;
Damitt sich die Buchstaben "von selbst tippen"
Delphi-Quellcode:
procedure dos(Eingabe: String; sleeper: integer);
var I:Integer;
const
  U = #13 + #10;
begin
  for I:=1 to Length(Eingabe) do
    begin
      form1.Memo1.Text:=form1.Memo1.Text+Eingabe[I];
      form1.Memo1.Refresh;
      Sleep(sleeper)
    end;
  form1.Memo1.Text:=form1.Memo1.Text + u;
end;
Kleine Spielerei, Fenster Transparent machen
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var trans: integer;
begin
  Present:= Now;
  DecodeDate(Present, Year, Month, Day);
  DecodeTime(Present, Hour, Min, Sec, MSec);
  trans:= form1.TrackBar1.Position * 2;
  trans:= trans div 5;
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), '>>> Ändere Programmtransparentz auf: '+ inttostr(trans) + '%');
  alphablendvalue:=form1.TrackBar1.Position;
  Alphablend:=true;
end;
Verbindung zur MySQL Datenbank;
Delphi-Quellcode:
procedure vermysql;
begin
  Present:= Now;
  DecodeDate(Present, Year, Month, Day);
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Setze MySQL-Port auf ' + form1.mysqlport.Text);
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Setze MySQL-Port auf ' + form1.mysqlport.Text, 60);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Verbinde zu MySQL-Host ['+form1.mysqlhost.Text+']...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Verbinde zu MySQL-Host ['+form1.mysqlhost.Text+']...', 60);
  form1.FMysql.Host := form1.mysqlhost.Text;
  form1.FMysql.port := strtoint(form1.mysqlport.text);

  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Sende MySQL-Username ['+ form1.mysqlusername.Text +']');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Sende MySQL-Username ['+ form1.mysqlusername.Text +']...', 60);
  form1.FMysql.user := form1.mysqlusername.Text;

  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Sende MySQL-Username ['+ form1.mysqlpassword.Text +']');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Sende MySQL-Password ['+ form1.mysqlpassword.Text +']...', 60);
  form1.FMysql.password := form1.mysqlpassword.Text;

  form1.FMysql.UnixSocket := '';
  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Öffne MySQL-Datenbank ['+form1.mysqldatenbankname.Text+'...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Öffne MySQL-Datenbank ['+ form1.mysqldatenbankname.Text +'...', 60);
  form1.FMysql.Db := form1.mysqldatenbankname.Text; // Datenbank auf dem Server

  form1.FMysql.UseNamedPipe := false;
  form1.FMysql.UseSSL := false;
  form1.FMysql.Compress := true;
  form1.FMysql.TrySockets := false;
  if form1.FMysql.Connect then
    begin
      Present:= Now;
      DecodeDate(Present, Year, Month, Day);
      DecodeTime(Present, Hour, Min, Sec, MSec);
      log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Verbindung zu MySQL-Datenbank erfolgreich!');
      dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Verbindung zu MySQL-Datenbank erfolgreich!', 60)
    end
  else
    begin
      Present:= Now;
      DecodeDate(Present, Year, Month, Day);
      DecodeTime(Present, Hour, Min, Sec, MSec);
      dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Verbindung zu MySQL-Datenbank felgeschlagen!', 60);
      log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Verbindung zu MySQL-Datenbank felgeschlagen!');
    end;
end;
Verbinde zum FTP-Server (Soll ja mal ein Hompagebackupprogramm werden)
Delphi-Quellcode:
procedure dlord;
begin
  form1.Show;
  form1.IdFTP1.Port:=strtoint(form1.port.Text);
  Present:= Now;
  DecodeDate(Present, Year, Month, Day);
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Setze FTP-Port auf '+ form1.port.Text +'...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Setze FTP-Port auf '+ form1.port.Text +'...', 60);
  sleep(100);

  form1.IdFTP1.Username:=form1.username.Text;
  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Übertrage FTP-Username ['+ form1.username.Text +']...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Übertrage FTP-Username ['+ form1.username.Text +']...', 60);
  sleep(100);

  form1.IdFTP1.Username:=form1.username.Text;
  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Übertrage FTP-Password ['+ form1.password.Text +']...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Übertrage FTP-Password ['+ form1.password.Text +']...', 60);
  sleep(100);

  form1.IdFTP1.Password:=form1.password.Text;
  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Verbinde mit FTP-Host ['+ form1.hostname.Text +']...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Verbinde mit FTP-Host ['+ form1.hostname.Text +']...', 60);
  sleep(100);

  form1.IdFTP1.Host:=form1.hostname.Text;
  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Warte auf FTP-Server...');
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Warte auf FTP-Server...', 60);
  try
  form1.IdFTP1.Connect;
  if form1.IdFTP1.Connected then
    begin
      Present:= Now;
      DecodeTime(Present, Hour, Min, Sec, MSec);
      log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'FTP-Verbindung Hergestellt.');
      dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] FTP-Verbindung Hergestellt.', 60);
    end;
  except
    Present:= Now;
    DecodeTime(Present, Hour, Min, Sec, MSec);
    log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'FTP-Verbindung konnte nicht Hergestellt werdern.');
    dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] FTP-Verbindung konnte nicht Hergestellt werdern.', 60);
  end;
end;
Zum Conecten und speicher des Feld Inhaltes
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
begin
  dlord;
  vermysql;
  
  sett.WriteString('FTP', 'Hostname', form1.hostname.Text);
  sett.WriteString('FTP', 'Port', form1.port.Text);
  sett.WriteString('FTP', 'Username', form1.username.Text);
  sett.WriteString('FTP', 'Password', form1.password.Text);

  sett.WriteString('MySQL', 'Hostname', form1.mysqlhost.Text);
  sett.WriteString('MySQL', 'Port', form1.mysqlport.Text);
  sett.WriteString('MySQL', 'Username', form1.mysqlusername.Text);
  sett.WriteString('MySQL', 'Password', form1.mysqlpassword.Text);
  sett.WriteString('MySQL', 'Datenbankname', form1.mysqldatenbankname.Text);

  form1.Button2.Enabled:= false;
  form1.Button3.Enabled:= true;
end;
Zum Disconnecten
Delphi-Quellcode:
procedure TForm1.Button3Click(Sender: TObject);
begin
  Present:= Now;
  DecodeDate(Present, Year, Month, Day);
  DecodeTime(Present, Hour, Min, Sec, MSec);
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Trenne Verbindung...', 60);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Trenne Verbindung...');
  if FMysql.Connected then
     FMysql.close;
  if form1.IdFTP1.Connected then
     form1.IdFTP1.Disconnect;
  Present:= Now;
  DecodeTime(Present, Hour, Min, Sec, MSec);
  dos('['+ inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec) +'] Verbindung getrennt!', 60);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), 'Verbindung getrennt!');
  form1.Button3.Enabled:= false;
  form1.Button2.Enabled:= true;
end;

Lesen der Voreinstellungen der settings.ini
Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
begin
  FMysql := TMysqlClient.create;
  sett:= TiniFile.Create(ExtractFilePath(ParamStr(0))+
                         'settings.ini');
  log:= TiniFile.Create(ExtractFilePath(ParamStr(0))+
                        'log.txt');
  Present:= Now;
  DecodeDate(Present, Year, Month, Day);
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), '>>> Programmstart');
  form1.hostname.Text:= sett.ReadString('FTP', 'Hostname', 'FTP-Hostname');
  form1.port.Text:= sett.ReadString('FTP', 'Port', 'FTP-Port');
  form1.username.Text:= sett.ReadString('FTP', 'Username', 'FTP-Username');
  form1.password.Text:= sett.ReadString('FTP', 'Password', 'FTP-Password');

  form1.mysqlhost.Text:= sett.ReadString('MySQL', 'Hostname', 'MySQL-Hostname');
  form1.mysqlport.Text:= sett.ReadString('MySQL', 'Port', 'MySQL-Port');
  form1.mysqlusername.Text:= sett.ReadString('MySQL', 'Username', 'MySQL-Username');
  form1.mysqlpassword.Text:= sett.ReadString('MySQL', 'Password', 'MySQL-Password');
  form1.mysqldatenbankname.Text:= sett.ReadString('MySQL', 'Datenbankname', 'MySQL-Datenbankname');
end;
Delphi-Quellcode:
procedure TForm1.FormDestroy(Sender: TObject);
begin
  Present:= Now;
  DecodeDate(Present, Year, Month, Day);
  DecodeTime(Present, Hour, Min, Sec, MSec);
  log.WriteString(inttostr(day)+'.'+inttostr(month)+'.'+inttostr(Year), inttostr(hour) +':'+ inttostr(min) +':'+ inttostr(sec), '>>> Programmende');
  FMysql.Free;
  if FResult<>nil then
    FResult.free;
end;

end.
Dass funktionirt ja auch so weit ich weis nur nicht weiter mit dem Backup!
Angehängte Dateien
Dateityp: rar back_191.rar (317,3 KB, 2x aufgerufen)
Maximilian Ruta
  Mit Zitat antworten Zitat