Recent

Author Topic: sqlite foreign keys  (Read 425 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
sqlite foreign keys
« on: January 03, 2023, 03:54:57 pm »
Does anybody know how to turn on FK checking in sqlite from fp?

In the sqlite documentnation I see that

PRAGMA foreign_keys = on

has to be called every time a new connection is opened, and has not to be done within transaction.


Currently I tried this 2 ways:

1. Create sqlite connection
2. Open sqlite connection
3. ExecuteDirect('PRAGMA foreign_keys = on');

This fails saying "Transaction not set."

1. Create sqlite connection
2. Open sqlite connection
3. Create a transaction, bu do not start it
3. ExecuteDirect('PRAGMA foreign_keys = on', transaction);

This fails saying "Error: attempt to implicitly start a transaction on Connection "", transaction ""."

Thanks





FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: sqlite foreign keys
« Reply #1 on: January 03, 2023, 04:03:20 pm »
Does anybody know how to turn on FK checking in sqlite from fp?

In the sqlite documentnation I see that

PRAGMA foreign_keys = on

has to be called every time a new connection is opened, and has not to be done within transaction.


Currently I tried this 2 ways:

1. Create sqlite connection
2. Open sqlite connection
3. ExecuteDirect('PRAGMA foreign_keys = on');

This fails saying "Transaction not set."

1. Create sqlite connection
2. Open sqlite connection
3. Create a transaction, bu do not start it
3. ExecuteDirect('PRAGMA foreign_keys = on', transaction);

This fails saying "Error: attempt to implicitly start a transaction on Connection "", transaction ""."

Thanks

Stop, I found in

Code: Pascal  [Select][+][-]
  1. procedure TSQLite3Connection.DoInternalConnect;
  2. var
  3.   filename: ansistring;
  4. begin
  5.   Inherited;
  6.   if DatabaseName = '' then
  7.     DatabaseError(SErrNoDatabaseName,self);
  8.   InitializeSQLite;
  9.   filename := DatabaseName;
  10.   checkerror(sqlite3_open_v2(PAnsiChar(filename),@fhandle,GetSQLiteOpenFlags,Nil));
  11.   if (Length(Password)>0) and assigned(sqlite3_key) then
  12.     checkerror(sqlite3_key(fhandle,PChar(Password),StrLen(PChar(Password))));
  13.   if Params.IndexOfName('foreign_keys') <> -1 then
  14.     execsql('PRAGMA foreign_keys =  '+Params.Values['foreign_keys']);
  15. end;
  16.  

I have to supply a

Code: Pascal  [Select][+][-]
  1. conn.Params.AddPair('foreign_keys', 'ON');
  2.  

To much Prosecco at new year's eve.
« Last Edit: January 03, 2023, 04:07:38 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: sqlite foreign keys
« Reply #2 on: January 03, 2023, 04:14:05 pm »
Why not adding a small note to the wiki about this?

https://wiki.freepascal.org/SQLite
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018