Recent

Author Topic: How to create a new TSQLQuery in a DataModule at runtime  (Read 6696 times)

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
How to create a new TSQLQuery in a DataModule at runtime
« on: October 23, 2017, 04:29:03 pm »
Has anyone any experience with creating TSQLQueries at runtime in an existing DataModule?
A simple example would be of great help.
Thanks!
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to create a new TSQLQuery in a DataModule at runtime
« Reply #1 on: October 23, 2017, 04:35:47 pm »
Code: Pascal  [Select][+][-]
  1. uses db, sqldb, ..... ;
  2.  
  3. Function QueryDB(Const aDB :TCustomConnection; aCommand :String; Const aTransaction :TSQLTransaction) :TSQLQuery;
  4. begin
  5.   Result := TSQLQuery.Create(nil);//<- set it to either self or the component that will free it when finished its job
  6.   Result.DataBase := aDB;
  7.   if Assigned(aTransaction) then Result.Transaction := aTransaction;
  8.   Result.SQL.Text := aCommand;
  9.   Result.Open;
  10. end;
  11.  
Make sure that you pass it only select statements no delete, update or metadata.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: How to create a new TSQLQuery in a DataModule at runtime
« Reply #2 on: October 23, 2017, 05:24:28 pm »
Thanks Taazz!
I'll play with this and see how far it will get me.

The idea I'm playing with is to create a Windows database program like the old but famous DOS program PC-File where a user can define its own table structures (which I store in a table definitions table).
So when the program starts only the dbconnection and the table containing the user-table definitions are opened in the OnCreate of the dataModule. After that I need to make a routine that creates the other SQLqueries in the DataModule from the data that is read from the definitions table.
A nice puzzle, that will keep me from doing bad things.  :D
« Last Edit: October 23, 2017, 05:28:16 pm by JanRoza »
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

 

TinyPortal © 2005-2018