Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:17:49+00:00 2026-06-17T18:17:49+00:00

Hello i’ve this function to update my Access DB using TUniQuery : var Res:Boolean;

  • 0

Hello i’ve this function to update my Access DB using TUniQuery :

var
  Res:Boolean;
begin
Res:=false;
  try
  with MyQuery do
  begin
    Active := false;
    SQL.Clear;
    SQL.Add('Update MYTABLE');
    SQL.Add('set username='+QuotedStr(NewUserName));
    SQL.Add(',password='+QuotedStr(NewPassword));
    SQL.Add('where username='+QuotedStr(ACurrentUserName));
    ExecSQL;
    Res:=true;
  end;
  except
  Res:=False;
  end ;
  Result:=Res;
end;

Is the use of Try … except enough to KNOW when ” ExecSQL ” succeeds or fails ?

or is there any other better approach ?

thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:17:50+00:00Added an answer on June 17, 2026 at 6:17 pm

    You may want to consider your update succeeded if no exception is raised. It means the database is responsive and parsed and executed your statement without syntax errors.

    In a statement like shown, you may also want to be sure that exactly one row was updated, because I assume that’s your intention.

    To check that, you can resort to the result of the ExecSQL method, which returns the number of rows affected by the execution of the statement. So, you may change your code to this:

    begin
      with MyQuery do
      begin
        Active := false;
        SQL.Clear;
        SQL.Add('Update MYTABLE');
        SQL.Add('set username='+QuotedStr(NewUserName));
        SQL.Add(',password='+QuotedStr(NewPassword));
        SQL.Add('where username='+QuotedStr(ACurrentUserName));
        Result := ExecSQL = 1; //exactly 1 row updated
      end;
    end;
    

    I also changed the unconditional exception handler, since it may not be the proper site to eat any exception, and also removed the local variable to store the Result, since that really is not necessary.

    After reading your added text and re-thinking your question:

    Is the use of Try … except enough to KNOW when ” ExecSQL ” succeeds or fails ?

    You really have to change your mind about exception handling and returning a boolean from this routine. Exceptions were introduced as a whole new concept on how to address exceptional and error situations on your programs, but you’re killing this whole new (and IMHO better) approach and resorting to the old way to return a value indicating success or failure.

    Particularly a try/exception block that eats any exception is a bad practice, since you will be killing exceptions that may be raised for too many reasons: out of memory, network problems like connection lost to database, etc.

    You have to re-think your approach and handle these exceptional or error conditions at the appropriate level on your application.

    My advise is:

    • change this from a function to a procedure, the new contract is: it returns only if succeded, otherwise an exception is raised.
    • if an exception occurs let it fly out of the routine and handle that situation elsewhere
    • raise your own exception in case no exactly 1 row is updated
    • change the query to use parameters (avoiding sql injection)

    The routine may look like this:

    procedure TMySecurityManager.ChangeUserNameAndPassword();
    begin
      MyQuery.SQL.Text := 'Update MYTABLE'
                  + '   set username = :NewUserName'
                  + '       , password = :NewPassword'
                  + ' where username = :username';
      MyQuery.Params.ParamByName('NewUserName').AsString := NewUserName;
      MyQuery.Params.ParamByName('NewPassword').AsString := NewPassword;
      MyQuery.Params.ParamByName('username').AsString := ACurrentUserName;
      if MyQuery.ExecSQL <> 1 then
          raise EUpdateFailed.Create('A internal error occurred while updating the username and password');
      //EUpdateFailed is a hypotetical exception class you defined.
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am using this code to get the data I need from 5
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
Hello I've got this query to get users by email, which is an unique
Hello guys i have been using JavaFx scene builder to build a gui that
hello evrey body i have try really hard to write a file to android
Hello my rendering is very glitchy on Tegra 3 devices.. any Idea how this
Hello i have simply function to read from file while(fscanf(fp, %255[a-zA-Z],test) == 1) {
Hello everyone and thanks for your help! I currently have this program that renames
Hello all and thanks for your time reading this. I need to verify certificates
Hello and thanks for the help. I am using the following code to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.