AnyDac - How to disconnect from in-memory sqlite db?

1.1k views Asked by At

I'm using in-memory SQLite databases with AnyDac on Delphi XE2. I noticed that my queries return results even when I forget to connect the database first after restarting the program, which is probably caused by the autoconnect capability of AnyDac. The thing is that I guess that this must also mean that the in-memory databases stay in memory even after the program itself has terminated, which is kind of a memory leak.

I looked through the AnyDac documentation and searched online, but I could not find any way of how I am supposed to disconnect from a database using AnyDac correctly. I noticed that when I call the "close" method of a TADConnection the sqlite file seems to stay open. I guess the same happens with my in-memory databases.

Can anyone please tell me how to completely close, disconnect from and remove a in-memory SQLite database in a correct and safe way?

1

There are 1 answers

0
Branko On

ADConnection.Close completely remove in-memory SQLite DB. With next ADConnection.Open, explicitly or implicitly, new empty in-memory DB is created.

This can be easy confirmed by simple test:

  ADConnection1.Open;
  ADConnection1.ExecSQL('create table TEST (A, B)');
  ADConnection1.ExecSQL('insert into TEST values (1, 2)');
  // show value of TEST.A
  ShowMessage(VarToStr(ADConnection1.ExecSQLScalar('select A from TEST')));
  ADConnection1.Close;
  ADConnection1.Open;
  // next statement generates exception - [FireDAC][Phys][SQLite] ERROR: no such table: TEST
  ShowMessage(VarToStr(ADConnection1.ExecSQLScalar('select A from TEST')));