?
Solved

Create Tables if they don't exist

Posted on 2000-01-29
15
Medium Priority
?
126 Views
Last Modified: 2010-04-04
Hi Guy's,

How would I  check if a table exsists when the user first cranks up my App, and if it does'nt then create the table.

I need to know how to check if the table exist's.

I know how to create the table.

Asw
0
Comment
Question by:Asw
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2409241
hi asw,
what database?
meikl
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2409302
Easy way to try open table in "try - except-end" block

function IsOpen : boolean;
begin
   try
     Table1.Open;
     result:=true;
  except
    result:=false;
  end;
end;

Usual it applicable for all types of  database. But if you work with Interbase, you can look for table in system Interbase table.
0
 
LVL 1

Author Comment

by:Asw
ID: 2409361
Hi Meikl,

Paradox.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 2409401
hi again,

a sample for paradox

procedure TForm1.Button1Click(Sender: TObject);
var sl : TStringList;
begin
  Table1.DatabaseName := 'NameOfAlias';
  Table1.TableName := 'NameOfTable';
  SL := TStringList.Create;
  session.GetTableNames(Table1.DatabaseName,'*.db',False,False,SL);
  if sl.Indexof(Table1.TableName) = -1 then
  begin
    Table1.FieldDefs.Add('ID',ftInteger,0,True);
    Table1.FieldDefs.Add('Firstname',ftString,50,False);
    Table1.FieldDefs.Add('GivenName',ftString,50,False);
    // and more fields
    Table1.IndexDefs.Add('IdIdx','Id',[ixPrimary,ixUnique]);
    //and more indexes
    Table1.CreateTable;
  end;
  SL.Free;
  Table1.Open;
end;

meikl
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2409421
for example suppose that your table name is Phone.db and it must exists in your application directory, you can write this code:

Main form's OnCreate event:

begin
  if not FileExists(ExtractFilePath(ParamStr(0)) + 'Phone.db') then
  CreatePhoneTable; // your procedure

  Table1.DatabaseName:= ExtractFilePath(ParamStr(0));
  Table1.TableName:= 'Phone.db';
end;

Motaz
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2409501
hi again,

for a better compare by different write-styles,
add/replace this part

....
  session.GetTableNames(Table1.DatabaseName,'*.db',False,False,SL);
  SL.Text := UpperCase(sl.Text);              //do a uppercase-compare
  if sl.Indexof(UpperCase(Table1.TableName)) = -1 then
  begin
....

remark:
my solution is path-independent

meikl
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2409781
This method assumes that you have a database component or Alias, but my method can be used without need to a database name or alias

Motaz
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2409941
hi motaz,

no database component is used,
only the aliasname, which is better to use
instead a hard coded path, but nayway
i didn't say that your solution is wrong,
its just unflexible

meikl
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2410181
It is not a hardcode path it is only

ExtractFilePath(ParamStr(0));

if you use Alias you have to create it when you deploy your application, but if you do not rely on Alias you do not need to create any alias and the deployment become easier

Motaz
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2410281
hi motaz,

thats a part of philosophy.
you may have a problem,
if the customer decides,
for backup- or security related reasons,
to divide the app and the tables
into diferent paths/servers

meikl
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2411541
I think we must first understand the whole problem befor answering.

Asw, what exactly did you want to do and how many tables did you have, and tell us about your current project.

Motaz
0
 
LVL 1

Author Comment

by:Asw
ID: 2419001
Hi Guy's,

Both the comments work perfect, I just needed to know the best and safest way to check if a table exists, then if it does not then I would create the table.

To be fair I need to give the points to the first correct ansewer, So I will have to give the points to Meikl.

So if you post a few lines a a ansewer
Meikl the points are yours.

Many Thanks Motaz, Itugay

Asw
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 2420201
well, ok asw,

glad to helped you,
good luck again

meikl ;-)
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2433521
Asw you can accept any comment as an answer after rejecting proposed answer, so that you do not need to tell any user to post any thing as an answer. This is a new feature of experts-exchange.

Good luck
Motaz
www.geocities.com/motaz1
0
 
LVL 1

Author Comment

by:Asw
ID: 2436541
Thanks Motaz,

Andy
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question