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
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
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.
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.
ASKER
Hi Meikl,
Paradox.
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(Tabl e1.Databas eName,'*.d b',False,F alse,SL);
if sl.Indexof(Table1.TableNam e) = -1 then
begin
Table1.FieldDefs.Add('ID', ftInteger, 0,True);
Table1.FieldDefs.Add('Firs tname',ftS tring,50,F alse);
Table1.FieldDefs.Add('Give nName',ftS tring,50,F alse);
// and more fields
Table1.IndexDefs.Add('IdId x','Id',[i xPrimary,i xUnique]);
//and more indexes
Table1.CreateTable;
end;
SL.Free;
Table1.Open;
end;
meikl
a sample for paradox
procedure TForm1.Button1Click(Sender
var sl : TStringList;
begin
Table1.DatabaseName := 'NameOfAlias';
Table1.TableName := 'NameOfTable';
SL := TStringList.Create;
session.GetTableNames(Tabl
if sl.Indexof(Table1.TableNam
begin
Table1.FieldDefs.Add('ID',
Table1.FieldDefs.Add('Firs
Table1.FieldDefs.Add('Give
// and more fields
Table1.IndexDefs.Add('IdId
//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
Main form's OnCreate event:
begin
if not FileExists(ExtractFilePath
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(Tabl e1.Databas eName,'*.d b',False,F alse,SL);
SL.Text := UpperCase(sl.Text); //do a uppercase-compare
if sl.Indexof(UpperCase(Table 1.TableNam e)) = -1 then
begin
....
remark:
my solution is path-independent
meikl
for a better compare by different write-styles,
add/replace this part
....
session.GetTableNames(Tabl
SL.Text := UpperCase(sl.Text); //do a uppercase-compare
if sl.Indexof(UpperCase(Table
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
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
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
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
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
Asw, what exactly did you want to do and how many tables did you have, and tell us about your current project.
Motaz
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Good luck
Motaz
www.geocities.com/motaz1
ASKER
Thanks Motaz,
Andy
Andy
what database?
meikl