![]() |
Datenbank: mysql • Version: 5.0 • Zugriff über: Delphi
Create Database and Table at runtime
Hi,
I would like to create Database and table in runtime, i have used Datamodule component and created two procedure, Procedure CreateDB; Procedure CreateTB;
Code:
can anybody help me please?
Procedure CreateDB;
begin // I would like to test if the "TestDB" not Exist then i would like to create the DataBase "TestDB" End; procedure TDataModule1.CreateTB; begin //I would like to check first if the table " titles" not exist then i would like to create this table Query1.Close; Query1.SQL.Add('CREATE TABLE titles( Query1.SQL.Add(' emp_no INT NOT NULL,'); Query1.SQL.Add('title VARCHAR(50) NOT NULL,'); Query1.SQL.Add(' from_date DATE NOT NULL, '); Query1.SQL.Add('to_date DATE,'); Query1.SQL.Add(' Primary KEY (emp_no),'); Query1.ExecSQL; end; |
AW: Create Database and Table at runtime
There is not a simple answer to this task.
You have would have to use a Query to check the dictionary of the database for the table in question. Therefor You need a Database connection. Which itself is in question. So You have to start with a connection to "Your" database. If this succeeds, speaking "connect succeeds", You probably like to check, if the database retrieved by given name is the one, You are looking for. Query for known tables, check Your own privileges. If everything is fine, start with your datamodel commands above. some dictionary links:
Code:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name' [AND table_name LIKE 'wild'] SHOW TABLES FROM db_name [LIKE 'wild'] ![]() |
AW: Create Database and Table at runtime
The only part being questionable is 'how do I know if a database exist?'.
Doing the same for tables (do they exist?) is easy as it is a built in function in the TADOConnection-component ('GetTableNames'). But, you need to connect to the database first in order to be able to use the TADOConnection. For MSSQL it's rather simple: 1. Connect to the master-DB of the server. 2. execute
Delphi-Quellcode:
to check for the existence of the DB.
SELECT * FROM master..sysdatabases where name = 'YourDatabase'
3. if it does not exists, create it using the 'CREATE DATABASE' command 4. connect to the database 5. use 'GetTableNames' to find out if the tables you need exist. 6. create them using the 'CREATE TABLE' command. It is very easy, once you've learned how to use google. Another very simple approach for SQL-Server is to create an empty database file and tell the connection to use it. In order to do that, simply start SSMS, create the database and the tables and detatch the DB. The file is now ready to use. For other RDBMS there are other approaches for points 1-3. |
AW: Create Database and Table at runtime
Zitat:
7, Create roles 8, Create/add user to role. |
AW: Create Database and Table at runtime
![]() Zitat:
|
AW: Create Database and Table at runtime
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:31 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz