Recent

Author Topic: TSQLite3Connection and ExecuteDirect - handling errors  (Read 675 times)

backprop

  • Full Member
  • ***
  • Posts: 101
TSQLite3Connection and ExecuteDirect - handling errors
« on: July 23, 2024, 09:49:35 am »
I'm using TSQLite3Connection and ExecuteDirect. Hoever, if there is an error in SQL command, it raised error.

What is the best way to handle this but also showing the error?

Definitely it is not good idea to raise an error...

Zvoni

  • Hero Member
  • *****
  • Posts: 2737
Re: TSQLite3Connection and ExecuteDirect - handling errors
« Reply #1 on: July 23, 2024, 10:20:58 am »
I'm using TSQLite3Connection and ExecuteDirect. Hoever, if there is an error in SQL command, it raised error.

What is the best way to handle this but also showing the error?

Definitely it is not good idea to raise an error...
ExecuteDirect should only be used for SQL-Statements NOT returning a result (if even that).
If you have an Error in your Statement (e.g. a Typo), why would it NOT be a good idea to raise an error?

That said: Wrap your Command in a Try/Except-Block
For the life of me, i can never remember if you have to exclude the Database-Exceptions in the IDE
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

backprop

  • Full Member
  • ***
  • Posts: 101
Re: TSQLite3Connection and ExecuteDirect - handling errors
« Reply #2 on: July 23, 2024, 01:46:31 pm »
The reason is simple - I have made replacement for command line SQLite3 and also have to execute commands in script manners (multiple SQL commands), similar as in any online SQL support sites doing.

It is just for testing purposes and performance. Thus raising errors all the time is just PITA. Errors should be output at console, memo or similar, not otherwise.
« Last Edit: July 23, 2024, 01:49:40 pm by backprop »

Zvoni

  • Hero Member
  • *****
  • Posts: 2737
Re: TSQLite3Connection and ExecuteDirect - handling errors
« Reply #3 on: July 23, 2024, 02:15:51 pm »
Then ExecuteDirect is the "wrong" method.

use "execsql" (protected method)
Look in sqlite3conn in line 927

Code: Pascal  [Select][+][-]
  1. procedure TSQLite3Connection.execsql(const asql: string);
  2. var
  3.  err  : pchar;
  4.  str1 : string;
  5.  res  : integer;
  6. begin
  7.  err:= nil;
  8.  Res := sqlite3_exec(fhandle,pchar(asql),nil,nil,@err);
  9.  if err <> nil then
  10.    begin
  11.    str1:= strpas(err);
  12.    sqlite3_free(err);
  13.    end;
  14.  if (res<>sqlite_ok) then
  15.    databaseerror(str1);
  16. end;

EDIT: Or better said: Use "execsql" as a template.
Write a Class helper for TSQLite3Connection with a Public (or whatever scope) Procedure called "ExecSQLiteDirect" (or whatever), using the code from "execsql"
I think you can see, it's basically the last line of "execsql" you would have to adjust
« Last Edit: July 23, 2024, 02:19:49 pm by Zvoni »
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

 

TinyPortal © 2005-2018