Forum > General

TSQLite3Connection and ExecuteDirect - handling errors

(1/1)

backprop:
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:

--- Quote from: backprop 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...

--- End quote ---
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

backprop:
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.

Zvoni:
Then ExecuteDirect is the "wrong" method.

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


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TSQLite3Connection.execsql(const asql: string);var err  : pchar; str1 : string; res  : integer;begin err:= nil; Res := sqlite3_exec(fhandle,pchar(asql),nil,nil,@err); if err <> nil then    begin   str1:= strpas(err);   sqlite3_free(err);   end; if (res<>sqlite_ok) then    databaseerror(str1);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

Navigation

[0] Message Index

Go to full version