Lazarus

Programming => Databases => Topic started by: Weitentaaal on November 17, 2020, 02:51:42 pm

Title: Error Handlicng when Table Doesn't Exists
Post by: Weitentaaal 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 :)
Title: Re: Error Handlicng when Table Doesn't Exists
Post by: Zvoni on November 17, 2020, 03:29:33 pm
Huh?

https://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.gettablenames.html

Why not just look it up beforehand?
Title: Re: Error Handlicng when Table Doesn't Exists
Post by: Weitentaaal on November 18, 2020, 10:31:55 am
i just searched for the Wrong Thing Sorry :o
Title: Re: Error Handlicng when Table Doesn't Exists
Post by: rvk 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.
Title: Re: Error Handlicng when Table Doesn't Exists
Post by: Weitentaaal on November 25, 2020, 10:26:26 am
Thanks rvk :)

Understand this whole thing better now.
I went with the gettablenames :)
TinyPortal © 2005-2018