Recent

Author Topic: [SOLVED] DataModule messed me up  (Read 319 times)

guest48180

  • Guest
[SOLVED] DataModule messed me up
« on: July 25, 2019, 02:35:03 pm »
On a form, I had a TSQLite3Connection, TSQLTransaction, TSQLTransaction, and a TSQLQuery. Life was good. I got it wired up and it was rolling. But then I decided to show off a little. So I added the TSQLite3Connection and the TSQLTransaction to a TDataModule, named DM1. On Unit1, I added
Code: Pascal  [Select]
  1. uses
  2.   Unit2;
Made all the necessary changes to the prior uses of TSQLite3Connection and TSQLite3Connection on Unit1, i.e.:
Code: Pascal  [Select]
  1. DM1.SQLite3Connection1.Connected := True;
  2.   DM1.SQLTransaction1.Active := True;
But now, when I run it, I get an error:
Code: Pascal  [Select]
  1. Project ProjectName raised exception class 'External SIGSEGV'
and references this line, which is also the first line in Unit1 that makes reference to my DM1 of Unit2:
Code: [Select]
DM1.SQLite3Connection1.DatabaseName := 'data.db';
Again, all of this worked until I started using the DataModule. I don't mind putting it all back on one form, but the DataModule seems kind of handy.

Here's all the code on Unit1:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, db, sqldb, sqlite3conn, Forms, Controls, Graphics, Dialogs,
  9.   DBGrids, DBCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     DataSource1: TDataSource;
  17.     DBGrid1: TDBGrid;
  18.     DBNavigator1: TDBNavigator;
  19.     SQLQuery1 : TSQLQuery ;
  20.     procedure DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34. uses
  35.   Unit2;
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);
  40. begin
  41.   case Button of
  42.   nbPost:
  43.     begin
  44.       SQLQuery1.ApplyUpdates;
  45.       DM1.SQLTransaction1.CommitRetaining;
  46.     end;
  47.   nbDelete:
  48.     begin
  49.       SQLQuery1.ApplyUpdates;
  50.       DM1.SQLTransaction1.CommitRetaining;
  51.     end;
  52.  
  53.   end;
  54. end;
  55.  
  56.  
  57.  
  58. procedure TForm1.FormCreate(Sender: TObject);
  59. begin
  60.   DM1.SQLite3Connection1.DatabaseName := 'data.db';
  61.   SQLQuery1.SQL.Text := 'PRAGMA foreign_keys = ON';
  62.   SQLQuery1.ExecSQL;
  63.   DM1.SQLTransaction1.Commit;
  64.  
  65.   SQLQuery1.SQL.Text :=
  66.     'CREATE TABLE IF NOT EXISTS customers(' +
  67.     'cust_id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
  68.     'cust_name VARCHAR(30) NOT NULL)';
  69.   DM1.SQLite3Connection1.Connected := True;
  70.   DM1.SQLTransaction1.Active := True;
  71.   SQLQuery1.ExecSQL;
  72.   DM1.SQLTransaction1.Commit;
  73.  
  74.   SQLQuery1.SQL.Text :=
  75.     'CREATE TABLE IF NOT EXISTS sites(' +
  76.     'site_id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
  77.     'site_name VARCHAR(30) NOT NULL, ' +
  78.     'site_cust VARCHAR(30) NOT NULL, ' +
  79.     'FOREIGN KEY (site_cust) REFERENCES customers(cust_id) '  +
  80.     'ON UPDATE CASCADE ON DELETE CASCADE)';
  81.   SQLQuery1.ExecSQL;
  82.   DM1.SQLTransaction1.Commit;
  83.  
  84.   SQLQuery1.SQL.Text := 'select * from customers';
  85.   SQLQuery1.Open;
  86.  
  87.   DBGrid1.Columns[0].Visible := False;
  88. end;
  89.  
  90. end.
  91.  

What am I doing wrong?

JanRoza

  • Hero Member
  • *****
  • Posts: 535
    • http://www.silentwings.nl
Re: DataModule messed me up
« Reply #1 on: July 25, 2019, 02:55:08 pm »
You're using DM1 regularly but is nowhere declared.
Whant's the name of your DataModule unit?
Add that name to your uses clause (so where you now have uses unit2, change it to uses unit2, <datamodule unit name>.
OS: Windows 10 (64 bit) / Ubuntu 19.04 (64 bit)
Laz: Lazarus 2.0.4 FPC 3.0.4 i386-win32-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 2075
Re: DataModule messed me up
« Reply #2 on: July 25, 2019, 03:38:46 pm »
You're using DM1 regularly but is nowhere declared.
Whant's the name of your DataModule unit?

From the original post, one may assume that unit2 is the DataModule unit and that DM1 is declared inside? If not it wouldn't compile, would it? There would be lots of "Identifier not found" compile-time errors.

The most probable cause of the error is that the form's OnCreate event is being triggered before the data module is (fully) created, so it references objects that don't exist yet. Check the creation order either in the "Project options" windows or directly in the program source (menu "Project->View project source").

Don't forget to check that the data module is on the auto-create list! If not, its objects will never be "alive" :)
« Last Edit: July 25, 2019, 03:41:16 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

guest48180

  • Guest
Re: DataModule messed me up
« Reply #3 on: July 25, 2019, 04:24:16 pm »
lucamar:

That was it. I moved the code to OnActivate for Form 1, and it runs good now. Thanks for pointing that out to me. That got me rolling again  :D

lucamar

  • Hero Member
  • *****
  • Posts: 2075
Re: [SOLVED] DataModule messed me up
« Reply #4 on: July 25, 2019, 04:52:22 pm »
Glad it helped. I've fallen on that trap quite a few times too :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.