Lazarus

Free Pascal => Database => Topic started by: JanRoza on October 23, 2017, 04:29:03 pm

Title: How to create a new TSQLQuery in a DataModule at runtime
Post by: JanRoza 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!
Title: Re: How to create a new TSQLQuery in a DataModule at runtime
Post by: taazz 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.
Title: Re: How to create a new TSQLQuery in a DataModule at runtime
Post by: JanRoza 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
TinyPortal © 2005-2018