Bootstrapping my database

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
steveastrouk01
Posts: 3
Joined: Tue 17 Apr 2018 18:49

Bootstrapping my database

Post by steveastrouk01 » Tue 17 Apr 2018 19:08

I have been a very happy Devart Delphi MySQL access components for many years, not a very GOOD user, I admit, but what I've previously written worked, and worked well.

Now I have been asked to add new functionality to my program, specifically creating code that will allow the program to determine if the database I access exists, and if it doesn't create it, create a user if one doesn't exist, assign privileges, and create all the tables, if they don't exist. Since the code is still under active development, I have a habit of adding new tables occasionally, and to be able to detect its non-existence and create a new table would also be good.

Previously, all this sort of thing I manage from MySQL administrator. I configured each of the bespoke machines individually, no problem.

I have managed to write a snippet that creates an instance of Tmyconnection to check if the DB exists, in the project level of my app, expecting I could access it in the application proper, instead of the component Tmyconnection I used in my main app, but it seems to be only local to the project code.

How else can I do this ? Tmycommand or Tmyscript have to be bound to an existing database connection.

Sorry to sound like a newbie, but I feel like one right now.

Steve

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Bootstrapping my database

Post by ViktorV » Wed 18 Apr 2018 08:49

Please write in more detail what is the essence of your question and what you mean by the phrase "to be only local to the project code".

steveastrouk01
Posts: 3
Joined: Tue 17 Apr 2018 18:49

Re: Bootstrapping my database

Post by steveastrouk01 » Thu 19 Apr 2018 19:03

Imagine I have a completely clean system, with only a working MYSQL installation. I install my Delphi app, it detects that there is no DB, and it needs to create a DB, tables and a user. How do I do that ?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Bootstrapping my database

Post by ViktorV » Mon 23 Apr 2018 12:10

To determine the existence of the database at the moment of connection, you can use the following code by preliminarily setting the TMyConnection.Database property to the required value:

Code: Select all

 MyConnection.Database := 'DBName';
  try
    MyConnection.Connect;
  except
    on E: Exception do
      if Pos('Unknown database', E.Message) < 0 then
        ShowMessage('Database "DBName" does not exist')
      else
        raise;
  end;
To execute the required scripts for creating a database and its required objects, you can use the TMyScript component: devart.com/mydac/docs/devart.mydac.tmyscript.htm

steveastrouk01
Posts: 3
Joined: Tue 17 Apr 2018 18:49

Re: Bootstrapping my database

Post by steveastrouk01 » Mon 23 Apr 2018 17:17

Thanks Viktor,
That was the clue I needed. Many thanks

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Bootstrapping my database

Post by ViktorV » Tue 24 Apr 2018 06:50

Thank you for the interest in our products.
If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.

Post Reply