Recent

Author Topic: Test if a table exists in Firebird?  (Read 20355 times)

cov

  • Full Member
  • ***
  • Posts: 222
Re: Test if a table exists in Firebird?
« Reply #15 on: April 10, 2013, 01:08:18 pm »
In the interests of helping anyone who has similar problems creating Tables for a Firebird Database, there are several errors to the code I posted above:

Code: [Select]
IBConnection1.ExecuteDirect('CREATE TABLE PATHS('+
              'ID INTEGER NOT NULL UNIQUE PRIMARY KEY, '+
              'PATH VARCHAR(800), '+
              'INCEPTION DATETIME,'+
              'UPDATE DATETIME,');

Should be:
Code: [Select]
  IBConnection1.ExecuteDirect('CREATE TABLE PATHS('+
              'ID INTEGER NOT NULL PRIMARY KEY, '+
              'PATH VARCHAR(800), '+
              'INCEPTION TIMESTAMP,'+
              'DWGUPDATE TIMESTAMP)');

So, to reitterate, use the following to check if a table exists in the database:
Code: [Select]
  tableList:= TStringList.Create;
  IBConnection1.GetTableNames(tableList,False);
  s:='MyTable';// Whatever the name of the table is.
  i:=0;
  while i<tableList.Count do
  begin
    if tableList.Strings[i]=s then
    begin
       break;
     end;
    inc(i);
  end;
  if i<tableList.Count then
  // implement the IBConnection1.ExecuteDirect code above.
  SQLTransaction1.Commit;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Test if a table exists in Firebird?
« Reply #16 on: April 10, 2013, 01:23:13 pm »
Thanks for posting that update!

FYI:
Code: [Select]
  tableList:= TStringList.Create;
  IBConnection1.GetTableNames(tableList,False);
  s:='MyTable';// Whatever the name of the table is.
// have a look at .IndexOf, something like (check docs though, this is air code)
  if tableList.IndexOf(s)=-1 then
  // implement the IBConnection1.ExecuteDirect code above.
  SQLTransaction1.Commit;
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018