AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Datenbanken Create Database and Table at runtime
Thema durchsuchen
Ansicht
Themen-Optionen

Create Database and Table at runtime

Ein Thema von question · begonnen am 6. Okt 2013 · letzter Beitrag vom 6. Okt 2013
Antwort Antwort
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#1

Create Database and Table at runtime

  Alt 6. Okt 2013, 12:52
Datenbank: mysql • Version: 5.0 • Zugriff über: Delphi
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:
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;
can anybody help me please?
  Mit Zitat antworten Zitat
jobo

Registriert seit: 29. Nov 2010
3.072 Beiträge
 
Delphi 2010 Enterprise
 
#2

AW: Create Database and Table at runtime

  Alt 6. Okt 2013, 13:09
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']
http://dev.mysql.com/doc/refman/5.0/...les-table.html
Gruß, Jo
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#3

AW: Create Database and Table at runtime

  Alt 6. Okt 2013, 15:28
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 SELECT * FROM master..sysdatabases where name = 'YourDatabase' to check for the existence of the DB.
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.
  Mit Zitat antworten Zitat
Benutzerbild von Bernhard Geyer
Bernhard Geyer

Registriert seit: 13. Aug 2002
17.171 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: Create Database and Table at runtime

  Alt 6. Okt 2013, 15:36
For MSSQL it's rather simple:
....
You missed some points
7, Create roles
8, Create/add user to role.
Windows Vista - Eine neue Erfahrung in Fehlern.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#5

AW: Create Database and Table at runtime

  Alt 6. Okt 2013, 15:36
http://dev.mysql.com/doc/refman/5.1/...-database.html
Zitat:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
There is a similar syntax for the CREATE TABLE-Statement as well.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Furtbichler
(Gast)

n/a Beiträge
 
#6

AW: Create Database and Table at runtime

  Alt 6. Okt 2013, 17:11
You missed some points..Create/add...
In a dynamic database? Which was created with admin credentials anyway? Well, yeah. Maybe.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:16 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz