Forum > Database
[SOLVED] Function result the content of a query
TheLastCayen:
Good point molly, I recompile using -gh and yup memory leak.
Since I have more function in my database class, I was not sure about creating the TZReadOnlyQuery in my constructor but if I have to choice between using unnecessary memory or a memory leak, I choice the unnecessary memory :-\
This is my code for future reference:
--- 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; dbexist := FileExists(dbname); DatabaseTZ := TZConnection.Create(Nil); QueryTZ := TZReadOnlyQuery.Create(Nil); QueryTZ.Connection := DatabaseTZ; ; with DatabaseTZ do begin Protocol := 'sqlite-3'; Database := dbname; Connect; end; if not dbexist then initializedb;end; destructor Tmydb.Destroy;begin QueryTZ.Close; QueryTZ.Free; DatabaseTZ.Disconnect; DatabaseTZ.free; inherited;end; function Tmydb.query(sqlcommand: AnsiString):tfields;begin with QueryTZ do begin Close; SQL.Clear; SQL.Add(sqlcommand); Open; Result := Fields; end;end;
This is how I use 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;'); while not tempresult.Dataset.Eof do begin showmessage(tempresult.FieldByName('name').AsString); tempresult.Dataset.Next; end; database.free;end;
Thank you.
ps: Big thank you for informing me about heaptrc. I now have to recompile bunch of my tools and I have the feeling few of them will make me cry ;)
molly:
--- Quote from: TheLastCayen on November 01, 2016, 02:24:24 am ---Good point molly, I recompile using -gh and yup memory leak.
--- End quote ---
Thanks for checking and reporting back.
--- Quote ---... but if I have to choice between using unnecessary memory or a memory leak, I choice the unnecessary memory :-\
--- End quote ---
Memory leaks are poison for apps, especially when they need to run 24/7. So, it is a wise decision ;D
--- Quote ---ps: Big thank you for informing me about heaptrc. I now have to recompile bunch of my tools and I have the feeling few of them will make me cry ;)
--- End quote ---
You're most welcome. It of course depends a little on your coding skills how happy you're going to be with that :P
In case this is your introduction to heaptrace, then please use it wisely. It is one of the best tools/options available to make sure your code is not doing something stupid.
While you're at it, make sure to set debug options range and overflow checking as well :)
totya:
--- Quote from: TheLastCayen on November 01, 2016, 01:30:24 am ---Thank you titya. Work like a charme.
--- End quote ---
Hi!
Your last code is will be better, if you use exception handle, for example:
--- 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:= nil; database:= tmydb.Create; if Assigned(database) then try tempresult:= database.query('SELECT * FROM testtable;'); while not tempresult.Dataset.Eof do begin showmessage(tempresult.FieldByName('name').AsString); tempresult.Dataset.Next; end; finally database.free; end;end;
But try/except block recommended too.
Otherwise, I think your code is slightly complicated, because if you want handle SQLite3, the TSqlite3Dataset is much easier to use. http://sqlite4fpc.yolasite.com/dataset-tutorial-1.php
TheLastCayen:
Hi totya.
Thank you for the recommendation, I will implement the exception handler.
My code can look complicate when you don't know what I am aiming for. 3 softwares will use my dbclass.pas.
1 GUI settings
1 CLI settings
1 core app running as deamon under linux or service under windows.
So I was looking for an easy way to centralize my database management and also a quick way to move from sqllite to postgres if I need later on.
I really appreciate how fast people jump in to help me solving my issue:)
Again Thank you everyone.
totya:
--- Quote from: TheLastCayen on November 01, 2016, 07:14:39 pm ---My code can look complicate when you don't know what I am aiming for. 3 softwares will use my dbclass.pas.
--- End quote ---
Hi!
The TSqlite3Dataset was a simple tip, nothing more. :)
Navigation
[0] Message Index
[*] Previous page