Recent

Author Topic: Hot to refer to a TSQLQuery component of a form?  (Read 3841 times)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Hot to refer to a TSQLQuery component of a form?
« on: November 21, 2014, 06:11:42 pm »
I 'm trying to make a global function (placed in a unit) for closing some forms.
Every form has a control TSQLquery named 'SQLquery'.
I 'm trying to do a TSQLquery.close:
Code: [Select]
procedure GlobalFormClose(Sender: TObject);
var f: TForm;
var c: TSQLQuery;
begin
  f:=TForm(Sender);
//c := f.Controls['SQLQuery']; FAILS
//c := f.f.FindComponent();['SQLQuery']; FAILS
//f.SQLQuery.close; FAILS

 
  f.SQLTransaction.Active := False;
  f.SQLite3Connection.Connected := False;

  f.Destroy;
  f:=nil;
end;         

They all fail:
c := f.Controls['SQLQuery']; FAILS
c := f.f.FindComponent();['SQLQuery']; FAILS
f.SQLQuery.close; FAILS
« Last Edit: November 21, 2014, 06:14:00 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Hot to refer to a TSQLQuery component of a form?
« Reply #1 on: November 21, 2014, 06:21:26 pm »
Is there a compelling reason for your queries to be coupled to your forms?
Have you tried containing your queries in a datamodule, which a form (if shown) could access?

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Hot to refer to a TSQLQuery component of a form?
« Reply #2 on: November 21, 2014, 06:30:40 pm »
I didn't think of this and also I have no idea how to do it  :-[.
Is there some example?

I found some, I 'll try, thanks!
« Last Edit: November 21, 2014, 06:43:44 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Hot to refer to a TSQLQuery component of a form?
« Reply #3 on: November 21, 2014, 07:45:08 pm »
No, I haven't made it work...
is there any working example?
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Hot to refer to a TSQLQuery component of a form?
« Reply #4 on: November 21, 2014, 11:24:51 pm »
Hi Panoss,
Below is what I am using to consolidate my interaction with SQLite.
Per my experience with sqldb the disconnect did not work the way I wanted so I switched.

Code: [Select]
unit SQLite_Utils_Zeos;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, ZConnection, ZDataset;

type
  Query = class
    private
    {SQLite DB Attributes}
    db_filename : string;
    db_driver   : string;

    {DB Objects}
    ZConnSQLite  : TZConnection;
    ZSQLiteQuery : TZQuery;
    public
      constructor Create(TheDBFile, TheSQLiteDLL : string);
      function RunQSQLite(AQuery : string) : boolean;
      function QuerySQLitebyTaskID(parameter: string; AQuery : string): string;
      function ReturnColumns(table_name: string):string;
 end;

implementation
    constructor Query.Create(TheDBFile, TheSQLiteDLL : string);
    begin
      db_filename := TheDBFile;
      db_driver   := TheSQLiteDLL;
    end;


    function Query.RunQSQLite(AQuery : string) : boolean;
    begin
        ZConnSQLite                := TZConnection.Create(nil);
        ZConnSQLite.Protocol       := 'sqlite-3';
        ZConnSQLite.Database       := db_filename;
        ZConnSQLite.LibraryLocation:= db_driver;
        ZConnSQLite.Connect;

        Result := ZConnSQLite.ExecuteDirect(AQuery);
        ZConnSQLite.CleanupInstance;
        ZConnSQLite.Disconnect;
        ZConnSQLite.FreeInstance;
    end;

    function Query.QuerySQLitebyTaskID(parameter: string; AQuery : string): string;

    var
      sql_text : string;

    begin
        ZConnSQLite                := TZConnection.Create(nil);
        ZConnSQLite.Protocol       := 'sqlite-3';
        ZConnSQLite.Database       := db_filename;
        ZConnSQLite.LibraryLocation:= db_driver;
        ZConnSQLite.Connect;

        ZSQLiteQuery            := TZQuery.Create(nil);
        ZSQLiteQuery.Connection := ZConnSQLite;

        if length(parameter) > 0 then sql_text := (AQuery + parameter) else sql_text := (AQuery);
        ZSQLiteQuery.SQL.Add(sql_text);
        ZSQLiteQuery.Open;

        Result := ZSQLiteQuery.Fields[0].AsString;

        ZSQLiteQuery.CleanupInstance;
        ZSQLiteQuery.Close;
        ZSQLiteQuery.FreeInstance;

        ZConnSQLite.CleanupInstance;
        ZConnSQLite.Disconnect;
        ZConnSQLite.FreeInstance;
    end;

    function Query.ReturnColumns(table_name : string):string;

    var
      list_of_columns : string = '';

    begin
        ZConnSQLite                := TZConnection.Create(nil);
        ZConnSQLite.Protocol       := 'sqlite-3';
        ZConnSQLite.Database       := db_filename;
        ZConnSQLite.LibraryLocation:= db_driver;
        ZConnSQLite.Connect;

        ZSQLiteQuery            := TZQuery.Create(nil);
        ZSQLiteQuery.Connection := ZConnSQLite;

        ZSQLiteQuery.SQL.Text:= 'PRAGMA table_info(' + table_name + ')';
        ZSQLiteQuery.Open;

        ZSQLiteQuery.Next; //skip first column which is the autincremented ID

        while not ZSQLiteQuery.EOF do
        begin
          if list_of_columns = '' then list_of_columns := '''' + ZSQLiteQuery.Fields[1].AsString + '''' else list_of_columns := list_of_columns + ',' + '''' + ZSQLiteQuery.Fields[1].AsString + '''';
          ZSQLiteQuery.Next;
        end;

        Result := list_of_columns;

        ZSQLiteQuery.CleanupInstance;
        ZSQLiteQuery.Close;
        ZSQLiteQuery.FreeInstance;

        ZConnSQLite.CleanupInstance;
        ZConnSQLite.Disconnect;
        ZConnSQLite.FreeInstance;
    end;
end.
Lazarus 2.0.2 64b on Debian LXDE 10

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Hot to refer to a TSQLQuery component of a form?
« Reply #5 on: November 22, 2014, 12:28:06 am »
Have a look at this one.

No, I haven't made it work...
is there any working example?
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Hot to refer to a TSQLQuery component of a form?
« Reply #6 on: November 22, 2014, 09:19:17 am »
Thank you all very mush, I finally made it work (I had just to adjust the datasource property of the data bound controls of the forms  :-[).
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

 

TinyPortal © 2005-2018