Mega Search
23.2 Million


Sign Up

Make a donation  
Deleting SQLite tables but database is locked  
News Group: embarcadero.public.delphi.database.firedac

Am developing a simple SQLite DB browser/editor using XE6/FireDAC.  When I attempt to delete a table, I get  "ERROR: Database table is locked if any other table is open, even though the browser is the only user of the database at this point.  Oddly enough, I can rename the table (just not delete it).  To delete it I have to close all open tables.  Here are my connection parameters:

  FDConnection1.Connected := false;
  FDConnection1.Params.Clear;
  FDConnection1.Params.Add('DriverID=SQLite');
  FDConnection1.Params.Add('DriverName=SQLite');
  FDConnection1.Params.Add('Database='+D);    // D is a string of the full path database name
  FDConnection1.Params.Add('Protocol=TCPIP');
  FDConnection1.Params.Add('Server=localhost');
  FDConnection1.Params.Add('PRAGMA locking_mode = NORMAL');
  FDConnection1.Params.Add('DateTimeFormat=DateTime');
  FDConnection1.Params.Add('PRAGMA journal_mode=TRUNCATE');
  FDConnection1.Params.Add('PRAGMA auto_vacuum=1');
  FDConnection1.Params.Add('PRAGMA page_size=65536');
  //FDConnection2.Params.Add('MetaDefSchema=MAIN');
  FDConnection1.Connected := true;

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 12-Jan-2015, at 1:53 PM EST
From: Larry X
 
Re: Deleting SQLite tables but database is locked  
News Group: embarcadero.public.delphi.database.firedac
Two points:
1) Set LockingMode=Normal at connection definition parameters.
2) Try to do FetchAll on the dataset, then try to delete the records.

-- 
With best regards,
Dmitry Arefiev / FireDAC Architect

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 15-Jan-2015, at 6:26 AM EST
From: Dmitry Arefiev
 
Re: Deleting SQLite tables but database is locked  
News Group: embarcadero.public.delphi.database.firedac
I was too hasty in my assessment.  The issue still persists, even though there is only one connection.  Oddly enough, if I do a recordcount on the open table (call it TableA), I can then delete another table (call it TableB) even though TableA is open.  If I attempt to delete TableB with TableA open without having done a recordcount on TableA, I get the database locked error message.  What's going on?

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 1:22 PM EST
From: Larry X
 
Re: Deleting SQLite tables but database is locked [Edit]  
News Group: embarcadero.public.delphi.database.firedac
Thank you Cristian and Dmitry.  Very helpful.  Issue solved.

Edited by: Larry X on Jan 13, 2015 9:31 AM

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 9:31 AM EST
From: Larry X
 
Re: Deleting SQLite tables but database is locked  
News Group: embarcadero.public.delphi.database.firedac
>   FDConnection1.Params.Add('Protocol=TCPIP');
>   FDConnection1.Params.Add('Server=localhost');

Remove. SQLite is not a client/server DBMS.

>   FDConnection1.Params.Add('PRAGMA locking_mode = NORMAL');
>   FDConnection1.Params.Add('PRAGMA journal_mode=TRUNCATE');
>   FDConnection1.Params.Add('PRAGMA auto_vacuum=1');
>   FDConnection1.Params.Add('PRAGMA page_size=65536');

FireDAC SQLite driver does not have such parameters. See for details:
http://docwiki.embarcadero.com/RADStudio/XE7/en/Connect_to_SQLite_database_(FireDAC)
Instead you can do:
{code}
  FDConnection1.ExecSQL('PRAGMA locking_mode = NORMAL');
  FDConnection1.ExecSQL('PRAGMA journal_mode=TRUNCATE');
  FDConnection1.ExecSQL('PRAGMA auto_vacuum=1');
  FDConnection1.ExecSQL('PRAGMA page_size=65536');
{code}

-- 
With best regards,
Dmitry Arefiev / FireDAC Architect

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 13-Jan-2015, at 7:12 AM EST
From: Dmitry Arefiev
 
Re: Deleting SQLite tables but database is locked  
News Group: embarcadero.public.delphi.database.firedac
>   //FDConnection2.Params.Add('MetaDefSchema=MAIN');
>   FDConnection1.Connected := true;
If you use two connections for the same database almost sure this is the reason.

Best regards,
Cristian Peța

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 12-Jan-2015, at 11:14 PM EST
From: Cristian Peta