Create Tables if they don't exist

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
LVL 1
AswAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi asw,
what database?
meikl
0
ITugayCommented:
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
AswAuthor Commented:
Hi Meikl,

Paradox.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

kretzschmarCommented:
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
MotazCommented:
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
kretzschmarCommented:
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
MotazCommented:
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
kretzschmarCommented:
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
MotazCommented:
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
kretzschmarCommented:
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
MotazCommented:
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
AswAuthor Commented:
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
kretzschmarCommented:
well, ok asw,

glad to helped you,
good luck again

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MotazCommented:
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
AswAuthor Commented:
Thanks Motaz,

Andy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.