0

I am still fairly new to Delphi and learning new things every day.

What I am trying to do is generate multiple MySQL queries into a .txt that I can have backed up for when I need them.

Basically I have the following setup.

A VCL program that currently at the click of a button captures the list of tables that exist on my one database.

The tables are listed into a ListBox and gives me a count of the total tables that I have.

Now how would I go about doing the following:

I want it to capture the name of the first item in the ListBox and then create a .txt file and insert the name of the item into a specific text string, eg:

mysqldump -uroot -pxxxx -D[]database [tablename] > [tablename]

The sections where it is in [] I need the item from the ListBox being inserted there and need this to repeat onto the next time.

I have 249 tables that I need to generate these queries for and someone suggested that I can do a Delphi app that can do this pretty quickly and automated for me. It is basically for a large scale table dump and then import.

I know this is really long winded and just looking for some guidelines and tips on how I can do this.

I am doing this locally and I do not wish to use myDAC or FireDAC I would like it to be done locally without needed to access the database through MySQL or anything of the such. Just want it to generate my queries to a .txt file.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • You might want to ask about your task on the DBA Stack Exchange site. You could ask "[How do I back up a MySQL database?](http://dba.stackexchange.com/q/30974/47721)" See also the Stack Overflow question about [dumping tables to separate files in a single command](http://stackoverflow.com/q/3669121/33732). – Rob Kennedy Sep 18 '14 at 17:40

3 Answers3

1

Try something like this:

var
  DBName, TableName: string;
  SL: TStringList;
begin
  DBName := 'yourdbname';
  TableName := ListBox1.Items[0];
  SL := TStringList.Create;
  try
    SL.Add(Format('mysqldump -uroot -pxxxx -D%s %s > %1:s', [DBName, TableName]));
    SL.SaveToFile('c:\folder\query.txt');
  finally
    SL.Free;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

IF I understood you right, you have a TListBox with table names from which you want to create queries. You did not

In that case, you need something like this:

var F : TexFile;
    I : integer;
begin
  AssignFile(F, 'queries.txt');
  Rewrite(F);
  for I := 0 to ListBox1.Items.Count - 1 do
    Writeln(F, 'mysqldump -uroot -pxxxx -D[database] ['+ListBox1.Items[I]+'] > ['+ListBox1.Items[I]+']');
  CloseFile(F);
end;
adlabac
  • 416
  • 4
  • 12
  • Why keep using the old file I/O stuff, especially for this simple task? A `TStringList` would be perfect for this use, and it has a `SaveToFile` method that would do the write all in one shot. (See Remy's much better answer for an implementation.) Not downvoting, but this is far from the best solution. – Ken White Sep 19 '14 at 13:43
  • Call me oldfashioned, but I really do not see much (or any) advantages of using `TStringList`... – adlabac Sep 19 '14 at 17:35
  • 1) Support for other than ANSI character sets. 2) It's much faster to simply call `SaveToFile`. 3) No need to `Rewrite` first. 4) Cross-platform support. 5) More modern, easier to maintain code than the 1990's file I/O routines. – Ken White Sep 19 '14 at 18:13
  • It's easier to simply call `SaveToFile` and no need to call `Rewrite`? Is it I/O better because there is no need to call `Create` and `Free`? How would you `Append` something to the end of the file - by reading the whole file and saving it whole again? Not to mention working with huge files and memory usage. The guy said he want to use it locally on his computer, so no need for cross-platform support, and I doubt that he is used non-ANSI characters for table names. But, if it's modern, what the heck... – adlabac Sep 19 '14 at 22:45
  • There's no indication of "huge files" here, because it's a *list of table names*. There's no indication that it's not planned on being cross-platform - "the guy" might be planning an app that works on his desktop and tablet or phone. Your code doesn't "append" either; it calls `Rewrite`, remember? I gave a list of 5 reasons I disagree with your answer, and you've offered no advantage to *not* using `TStringList` instead (other than you have no desire to move away from 1990's code). But hey - if you want to live in the 90's, be my guest. I still think Remy's answer is a better solution. – Ken White Sep 20 '14 at 23:07
0

To make strings like that I would suggest using the format function. All you'll need is the SysUtils unit.

It would make your code look like this:

Table1 := 'FirstTable';
Table2 := 'SecondTable';
Format('mysqldump -uroot -pxxxx -D[]database %s > %s', [Table1, Table2]);
//Results in mysqldump -uroot -pxxxx -D[]database FirstTable > SecondTable

Just simply replace the Table1 and Table2 with the text of the selected item in your listbox.

Hope this helped you in any way :)

Teun Pronk
  • 1,367
  • 12
  • 24