[SOLVED] DataModule Woes lazarus / 16-Databases ---------------------------------------------------------------------- gmold · 2022-03-18 07:50 Hello Everyone, I've been working through Chapter 13 in the Lazarus Handbook.  The author strongly recommends using a data module so I set up an SQLite3 DB with one table and populated it with a few rows.   I've attached an image of my screen (Rasberry PI, Lazarus 2.0.10 FPC 3.2.0) of what I have done. As you can see in the image, the data connection is active in the forms designer as the DBGrid has rows , but when I compile and run the form the DBGrid does not get populated.  I've tried to activate the connection/query in the datamodulecreate event procedure but no luck. The code is in the imgage. I've experimented with moving he code to the  form oncreate but that gave me SIGSEGV  error.  If I remove the data module and drop the connector, transaction , query etc on the form it works fine. Q. Is there some flag I'm supposed to set to true at design time? Q. Can you see anything in my code that I have missed? Glenn, Terra Australis ---------------------------------------------------------------------- mig-31 · 2022-03-18 08:54 SQLite is a single user database. Your database is locked by the Lazarus. Deactive SQLQuery in Lazarus. ---------------------------------------------------------------------- gmold · 2022-03-18 09:26 Hi, Thank you, I  deactivated everything, see the attached image, you can see in the attached image that the DBGrid in designer is empty but the running app is too.  Glenn. ---------------------------------------------------------------------- dseligo · 2022-03-18 09:37 After line 44 add following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i If dm1.qElection.Eof then   ShowMessage('Eof')else  ShowMessage(dm1.qElection.FieldByName('Country').AsString; If you get 'Eof' or some error, then problem isn't in DBGrid. What is your Lazarus and OS version? What is bitness of Lazarus and bitness of compiled program? Compile your program in same bitness as Lazarus (maybe your program is compiled in 64-bit and you have 32-bit libraries or vice versa). It would be easier if you could post compileable test project. ---------------------------------------------------------------------- gmold · 2022-03-18 10:19 Hi, I've added the code but I did not get the any message box when I ran the program.  I'm running on Rasbian,  Lazarus 2.0.10.dfsg-4 fpc 3.2.0.     I've tried 3 times to upload a zip file containing the lazarus project and failed. Glenn. ---------------------------------------------------------------------- Thaddy · 2022-03-18 10:24 Quote from: mig-31 on March 18, 2022, 09:54:07 amSQLite is a single user database. Your database is locked by the Lazarus. Deactive SQLQuery in Lazarus. That is nonsense. Please ignore. e.g. mormot. ---------------------------------------------------------------------- howardpc · 2022-03-18 10:39 Quote from: gmold on March 18, 2022, 11:19:07 amI've tried 3 times to upload a zip file containing the lazarus project and failed. Did you use the "Project -> Publish project" option using the IDE? You have to ensure that the zip is below a certain size (I think about 250k). So make sure there are no binaries or executables in the zip - source text files only. If your project requires a sqlite db file this will probably need to be a separate zip upload unless it is a very small dummy example just to show the problem. ---------------------------------------------------------------------- winni · 2022-03-18 10:51 Hi! The Forum increased the maximum attachment size from 1990th leven (250 kB) to 2000th level (500 kB). Winni ---------------------------------------------------------------------- dseligo · 2022-03-18 10:53 Quote from: gmold on March 18, 2022, 11:19:07 amHi, I've added the code but I did not get the any message box when I ran the program. Maybe DataModuleCreate isn't called. If you succeed in sending your test project, someone can look into it. Try to put button on your form, double click on it and put following code between 'begin' and 'end;' so the result is something like that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  ShowMessage('start');  dm1.qElection.Close;  dm1.qElection.SQL.Text := 'select * from elections';  dm1.cElection.Connected := True;  dm1.trElection.Active := True;  dm1.qElection.Active := True;  dm1.qElection.Open;  If dm1.qElection.Eof then    ShowMessage('Eof')  else    ShowMessage(dm1.qElection.FieldByName('Country').AsString;end; When you start program and click on the button you should first see message 'start' and then the other message. ---------------------------------------------------------------------- gmold · 2022-03-18 11:00 Hi, I've run the 'publish project' and the attached file is below.  I've not included the sqlite database which is only one table as per the post above. Note that it's late on Friday evening here in Australia and so I'll be offline for about 8 hours.  Good night. Glenn. ---------------------------------------------------------------------- dseligo · 2022-03-18 11:24 Your datamodule is not in auto create. Go to Project / Project Options / Forms and change it (like in the attachment). ---------------------------------------------------------------------- gmold · 2022-03-18 11:33 @dseligo, I did what you suggested and it worked!   I don't understand what has happened and why that worked. Can you explain? Glenn ---------------------------------------------------------------------- howardpc · 2022-03-18 11:38 Your .lpr needs to contain a line such as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Application.CreateForm(Tdm1, dm1);to create the datamodule before the main form is created. The Project Options dialog is a GUI way to ensure that this line is present in the .lpr (or you can add it manually). ---------------------------------------------------------------------- dseligo · 2022-03-19 04:55 Quote from: gmold on March 18, 2022, 12:33:31 pmI don't understand what has happened and why that worked. Can you explain? Forms and data modules are classes. To be able to use them you need to create them (instantiate). You can do this manually (e.g. dm1 := Tdm1.Create(nil); somewhere in main form), or you can let Lazarus auto create it. Event 'OnCreate' is called when data module is created. So, only thing missing in your application was creation of data module.