Recent

Author Topic: Untraceable access violation when opening a sqlite db the 2nd time  (Read 2795 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
Untraceable access violation when opening a sqlite db the 2nd time
« on: September 29, 2021, 08:15:59 pm »
Hi,

I am using this code to open a sqlite DB:
Code: Pascal  [Select][+][-]
  1. function TDBHelper.OpenDB(fn: String): Boolean;
  2. var s: String;
  3. begin
  4.     SQLite3Connection.DatabaseName := fn;
  5.     try
  6.       SQLite3Connection.Open;
  7.       if SQLite3Connection.Connected then
  8.       begin
  9.         SQLTransaction.Active := True;
  10.       end
  11. ...
  12.  

Then I am executing SELECT statements on it which works fine.

Then, after closing the DB via SQLite3Connection.Close; I am running into access violations when I try to (re)open the database.

What am I doing wrong?

Thx,
--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #1 on: September 29, 2021, 09:43:28 pm »
What am I doing wrong?

Not attaching a complete program that people can use to duplicate your problem.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #2 on: September 29, 2021, 10:05:52 pm »
I never used sqllite with transaction, but maybe close transaction first ?

Maybe the transaction component is left in some indefinite state.

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #3 on: September 30, 2021, 03:07:13 pm »
Attached is my small test project, but unfortunately, that even throws an error before the "interesting part": Access violation reading... in line 46 of tdbhelper. Don't know, what's going wrong here, it works in my main program.

If it runs, you should be able to reproduce the error by "open" -> "close" -> "open" buttons...

--Dietmar
« Last Edit: September 30, 2021, 03:53:17 pm by dietmar »
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

balazsszekely

  • Guest
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #4 on: September 30, 2021, 05:14:30 pm »
Hi dietmar,

Quote
Attached is my small test project, but unfortunately, that even throws an error before the "interesting part": Access violation reading... in line 46 of tdbhelper. Don't know, what's going wrong here, it works in my main program.

Form DBHelper is nil. You have to choices:
    1. Add DBHelper to the auto-create forms(project options)
    2. Create the form before calling method OpenDB:
Code: Pascal  [Select][+][-]
  1.  
  2.   dbHelper := TdbHelper.Create(Application);  //this line
  3.   if (DBHelper.OpenDB(ExtractFilePath(Application.Location) + 'test.db')) then begin
  4.     Query := TSQLQuery.Create(nil);
  5.     try
  6.       Query := DBHelper.SelectQuery('SELECT * FROM Owner LIMIT 1;');
  7.       StaticText1.Caption := Query.FieldByName('Wiz_Name').AsString;
  8.     finally
  9.       Query.Free;
  10.     end;
  11.   end;


dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #5 on: September 30, 2021, 06:25:43 pm »
Ok, after adding the form the the loadings, one can start testing with the real problem/error... thx, GetMem.

--Dietmar
« Last Edit: September 30, 2021, 06:27:29 pm by dietmar »
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

balazsszekely

  • Guest
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #6 on: September 30, 2021, 08:00:14 pm »
@dietmar
Quote
Ok, after adding the form the the loadings, one can start testing with the real problem/error... thx, GetMem.

See attached project. Instead of TdbHelper(TForm), please use a TDataModule.

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Untraceable access violation when opening a sqlite db the 2nd time
« Reply #7 on: September 30, 2021, 08:29:22 pm »
Thanks a lot for your efforts!
And I will start to read into TDataModule now...

--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

 

TinyPortal © 2005-2018