Link to home
Start Free TrialLog in
Avatar of Asw
Asw

asked on

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
Avatar of kretzschmar
kretzschmar
Flag of Germany image

hi asw,
what database?
meikl
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.
Avatar of Asw
Asw

ASKER

Hi Meikl,

Paradox.
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
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
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
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
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
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
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
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
Avatar of Asw

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Asw

ASKER

Thanks Motaz,

Andy