Shane David Alcock
Shane David Alcock

Reputation: 3

Delphi ListBox to a Text file with a specific string

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.

Upvotes: 0

Views: 1293

Answers (3)

adlabac
adlabac

Reputation: 416

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;

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 595329

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;

Upvotes: 1

Teun Pronk
Teun Pronk

Reputation: 1399

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 :)

Upvotes: 0

Related Questions