Forum > Databases
How te create a functions which returns a Query (or datasource)
Hansvb:
Hi,
I would like to create a function that returns a query (or a datasource).
What I have so far is below.
Main screen is under a temporary test button:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFrm_Main.Button1Click(Sender: TObject);var MaintainAppdatabase : TMaintainAppdatabase; Query : TSQLQuery; ds : tDataSource;begin try MaintainAppdatabase := TMaintainAppdatabase.Create(); Query := TSQLQuery.Create(nil); Query := MaintainAppdatabase.SelectAllItems(); // Query gets empty. <<-- this goes wrong SIGSEGV ds := tDataSource.Create(nil); ds.DataSet := Query; DBGridMain.DataSource := ds; if assigned(Query) then Query.Free; ds.free; MaintainAppdatabase.Free; finally // TODO; end;end;
In a separate unit (MaintainAppdatabase) is the function that the query or data source should return. That is:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TMaintainAppdatabase.SelectAllItems() : TSQLQuery;var selectSql : String; Query : TSQLQuery;begin selectSql := 'select * from ' + TableName.ITEMS; try SQLite3Conn.Close(); SQLite3Conn.DatabaseName := AppDatabaseFile; SQLite3Conn.Open; SQLTransaction.Active := True; Query := TSQLQuery.Create(nil); Query.SQL.Text := selectSql; Query.DataBase := SQLite3Conn; Query.Open; ResultQuery := Query; // ResultQuery is a TSQLQuery Result := ResultQuery; Query.Close; SQLite3Conn.Close(); Query.Free; SQLite3Conn.Close(); except on E : Exception do begin Frm_Main.Logging.WriteToLogError('Fout opgetreden bij het lezen van de tabel ' + TableName.ITEMS); Frm_Main.Logging.WriteToLogError(rsMessage); Frm_Main.Logging.WriteToLogError(E.Message); end; end;end;
Where am I going wrong?
Zvoni:
First codeblock, Line 9: Remove it
Hansvb:
When i remove that line then de SIGSEGV is gone. But the function is still wrong. Query gets empty in the second code block. Line 24 in the second code block is the problem.
MarkMLl:
--- Quote from: Hansvb on June 17, 2022, 02:18:57 pm ---When i remove that line then de SIGSEGV is gone. But the function is still wrong. Query gets empty in the second code block. Line 24 in the second code block is the problem.
--- End quote ---
Because you've just freed whatever your result points to.
MarkMLl
Hansvb:
I know. How can I avoid that or first make a copy? before I free the query.
My workaround will be the use of a data module I think.
Navigation
[0] Message Index
[#] Next page