Recent

Author Topic: Run SqlQuery1 using button  (Read 2651 times)

Bui Thiet

  • New member
  • *
  • Posts: 6
Run SqlQuery1 using button
« on: January 31, 2020, 04:21:20 pm »
How to Run SqlQuery1 using button

JanRoza

  • Hero Member
  • *****
  • Posts: 570
    • http://www.silentwings.nl
Re: Run SqlQuery1 using button
« Reply #1 on: January 31, 2020, 05:30:33 pm »
Depends on what you want to do when the button is clicked.
If you want to make another selection from a table code could look like this:

Code: Pascal  [Select][+][-]
  1. procedure button1.click
  2. var
  3.   strSQL: String;
  4. begin
  5.   strSQL := 'SELECT * FROM <table-name>';
  6.   SQLQuery1.Active := false;
  7.   SQLQuery1.SQL.Text := strSQL;
  8.   SQLQuery1.Active := true;
  9. end;

Replace <table-name> by the real name of the table you want to use.
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
Laz: Lazarus 2.0.8 FPC 3.0.4 i386-win32-win32/win64
       Lazarus 2.1.0 FPC 3.3.1 i386-win32-win32/win64
       CodeTyphon 7.2 FPC 3.3.1 i386-win32-win32/win64

Bui Thiet

  • New member
  • *
  • Posts: 6
Re: Run SqlQuery1 using button
« Reply #2 on: February 01, 2020, 04:12:58 pm »

Thanks JanRoza and it worked fine, it was great

Bui Thiet

  • New member
  • *
  • Posts: 6
Re: Run SqlQuery1 using button
« Reply #3 on: February 01, 2020, 04:59:53 pm »
How to compare values ​​in Edit1 with values ​​with Date Columns of type Date in Database. I want to sum by condition.

Sincerely thank you, hope you will help me.

JanRoza

  • Hero Member
  • *****
  • Posts: 570
    • http://www.silentwings.nl
Re: Run SqlQuery1 using button
« Reply #4 on: February 02, 2020, 12:44:26 am »
How about:

Code: Pascal  [Select][+][-]
  1. // Check edit1 against a database date
  2. if StrToDate(edit1.Text) <= SQLQuery1.FieldByName('<Date fieldname>').asString
  3. then.......;
  4.  

Search this forum for more info on working with databases, especially wiki pages. You can learn a lot from them.
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
Laz: Lazarus 2.0.8 FPC 3.0.4 i386-win32-win32/win64
       Lazarus 2.1.0 FPC 3.3.1 i386-win32-win32/win64
       CodeTyphon 7.2 FPC 3.3.1 i386-win32-win32/win64

Bui Thiet

  • New member
  • *
  • Posts: 6
Re: Run SqlQuery1 using button
« Reply #5 on: February 03, 2020, 03:20:31 pm »
Thank you for your help, I have another problem, I want to create 'Save'update'Delete,', 'I'm a new person so my ability is still limited. Oh, any help would mean a lot to me. Thanks very much.

rvk

  • Hero Member
  • *****
  • Posts: 3983
Re: Run SqlQuery1 using button
« Reply #6 on: February 03, 2020, 04:25:17 pm »
A few remarks...

An UPDATE SQL statement doesn't have := as assignment.
So it is
Code: SQL  [Select][+][-]
  1. UPDATE Danhmuc SET St=:Stt, etc

Note that the :Stt is in place of the parameter (that's what the : prefix is for) and there is no : before the =

But... you don't need all that.
If all is correct, the SQLQuery1.DeleteSQL and SQLQuery1.InsertSQL should already be set automatically.

So you can just do the following.
(and because you are using TDBEdits you don't need to use ParamByName etc.)

Code: Pascal  [Select][+][-]
  1. procedure Tfrmdanhmuc.ButtonDeleteClick(Sender: TObject);
  2. begin
  3.   SQLQuery1.Delete;
  4. end;
  5.  
  6. procedure Tfrmdanhmuc.ButtonDeleteClick(Sender: TObject);
  7. begin
  8.   SQLQuery1.Insert;
  9.   // a new row is inserted
  10.   // everything you change now in the DBGrid will be changed
  11.   // and you need to press a Update-button to really post the changes
  12. end;
  13.  
  14. procedure Tfrmdanhmuc.ButtonDeleteClick(Sender: TObject);
  15. begin
  16.   SQLQuery1.Post;
  17.   SQLQuery1.ApplyUpdates;
  18. end;


JanRoza

  • Hero Member
  • *****
  • Posts: 570
    • http://www.silentwings.nl
Re: Run SqlQuery1 using button
« Reply #7 on: February 03, 2020, 04:54:17 pm »
Bui Thiet,

See this sample project https://www.silentwings.nl/data/Kaartenbak.zip which is a simple cardfile system using SQLite that I used in a magazine tutorial some years ago.
Comments are in Dutch I'm afraid but It might help you a bit anyway.
 
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
Laz: Lazarus 2.0.8 FPC 3.0.4 i386-win32-win32/win64
       Lazarus 2.1.0 FPC 3.3.1 i386-win32-win32/win64
       CodeTyphon 7.2 FPC 3.3.1 i386-win32-win32/win64

Bui Thiet

  • New member
  • *
  • Posts: 6
Re: Run SqlQuery1 using button
« Reply #8 on: February 04, 2020, 06:05:48 am »

Thank you all, I will do it now.

 

TinyPortal © 2005-2018