Recent

Author Topic: Error Handlicng when Table Doesn't Exists  (Read 1505 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Error Handlicng when Table Doesn't Exists
« on: November 17, 2020, 02:51:42 pm »
Hey Guys,

So i Got This Code:

Code: Pascal  [Select][+][-]
  1. try
  2.      MacAd := dbHist.FieldByName('ProgID').AsString;
  3. except
  4.       on E : EDatabaseError do begin
  5.            //ShowMessage('DB ERROR:' + ' ' + E.ClassName + ' ' + E.Message);
  6.           //If This Table (RollBK) Doesnt Exists, then create it -->
  7.           //SQL := 'CREATE TABLE ROLLBK (Td DATE, Tt TIME, Zeichnung INTEGER, Part INTEGER, Act INTEGER, Pid TEXT(20));';
  8.       end;
  9.    end;
  10.  

Any1 any Idea how i Can do this ?

Thanks A Lot :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Error Handlicng when Table Doesn't Exists
« Reply #1 on: November 17, 2020, 03:29:33 pm »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Error Handlicng when Table Doesn't Exists
« Reply #2 on: November 18, 2020, 10:31:55 am »
i just searched for the Wrong Thing Sorry :o
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Error Handlicng when Table Doesn't Exists
« Reply #3 on: November 18, 2020, 10:38:48 am »
And besides that... you have the try/except around the wrong code. That will only trigger the except if there is no FIELD ProgID.

If you use a table which doesn't exists (like stated in your title) you need to put the sql.open (with SELECT) inside try/except. Then you can examine the exception if it contains "table not found" or something (depending on your database).

Checking beforehand with gettablenames would indeed be preferable over exceptionhandling.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Error Handlicng when Table Doesn't Exists
« Reply #4 on: November 25, 2020, 10:26:26 am »
Thanks rvk :)

Understand this whole thing better now.
I went with the gettablenames :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

 

TinyPortal © 2005-2018