Forum > Database
[SOLVED] Function result the content of a query
TheLastCayen:
Hi,
I am using:
Lazarus 1.6
FPC 3.0.0
ZEOSDBO 7.1.4
I am building a db class to manage my connection with a sqllite database. I wonder if there is a way to return the result of a query trough a function? I tried to use without success Tfields type for my function but it keep crashing on me. This is my code:
--- 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";}};} ---constructor Tmydb.Create;var dbexist : boolean; //Use to know if we build the databasebegin inherited; //DeleteFile(dbname); dbexist:=FileExists(dbname); DatabaseTZ:= TZConnection.Create(Nil); with DatabaseTZ do begin Protocol := 'sqlite-3'; Database := dbname; Connect; end;end; destructor Tmydb.Destroy;begin DatabaseTZ.Disconnect; DatabaseTZ.free; inherited;end; function Tmydb.query(sqlcommand: AnsiString):TFields;var Querry: TZReadOnlyQuery;begin Querry := TZReadOnlyQuery.Create(nil); with Querry do begin Connection := DatabaseTZ; SQL.Clear; SQL.Add(sqlcommand); Open; Result := Fields; Close; Free; end;end;
And this how I'am trying to call for it:
--- 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";}};} --- var database: tmydb; tempresult: TFields;begin database:= tmydb.Create; tempresult := database.query('SELECT * FROM testtable;'); database.free;end;
Thank you
molly:
I'm not so much into databases but, from my understanding:
--- Quote from: TheLastCayen on October 31, 2016, 10:27:43 pm ---
--- 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";}};} --- Querry := TZReadOnlyQuery.Create(nil); with Querry do begin... Result := Fields;... Free; end;
--- End quote ---
You free the querry and expect the fields property to stay alive ?
totya:
It seems to me it's true. The solution for example:
http://stackoverflow.com/questions/16846686/memory-leaks-when-returning-tfields-from-a-function
TheLastCayen:
--- Quote from: totya on November 01, 2016, 01:09:24 am ---It seems to me it's true. The solution for example:
http://stackoverflow.com/questions/16846686/memory-leaks-when-returning-tfields-from-a-function
--- End quote ---
Thank you titya. Work like a charme.
For future reference this is my code now:
--- 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 Tmydb.query(sqlcommand: AnsiString):TZReadOnlyQuery;begin Result := TZReadOnlyQuery.Create(nil); try Result.Connection := DatabaseTZ; Result.SQL.Add(sqlcommand); Result.Open; except Result.Close; Result.Free; raise; end;
And the way I call it:
--- 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";}};} ---var database: tmydb; tempresult: TFields;begin database:= tmydb.Create; tempresult:=database.query('SELECT * FROM testtable;').Fields; showmessage('Items found in testtable: ' + inttostr(tempresult.Count)); database.free;end;
molly:
--- Quote from: TheLastCayen on November 01, 2016, 01:30:24 am ---And the way I call it:
--- 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";}};} ---var database: tmydb; tempresult: TFields;begin database:= tmydb.Create; tempresult:=database.query('SELECT * FROM testtable;').Fields; showmessage('Items found in testtable: ' + inttostr(tempresult.Count)); database.free;end;
--- End quote ---
afaik that still gives you memory leakage for the query ?
Please double-check with heaptrc unit in debugging options.
Edit: on top of that, the code is assuming the result to always be valid while doing exception handling returning an invalid result. Very hard to debug in such one-liners in case things go wrong. I understand you went for convenience there but, that is not always possible.
Navigation
[0] Message Index
[#] Next page