Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Databases
»
[SOLVED] Relational database
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
How to have a generic enu...
by
PascalDragon
[
Today
at 11:07:46 pm]
WebLaz corrections
by
edgarrod71
[
Today
at 10:54:41 pm]
An ASCII logo for Pascal ...
by
domasz
[
Today
at 10:53:50 pm]
printing stuff
by
wp
[
Today
at 10:35:51 pm]
Flickering with TCustomCo...
by
deadbeef
[
Today
at 09:46:03 pm]
TGroupBox WMPaint ...
by
Espectr0
[
Today
at 06:52:13 pm]
[SOLVED] Timer
by
Pe3s
[
Today
at 06:30:34 pm]
Use of FreePascal package...
by
Thaddy
[
Today
at 06:05:51 pm]
Dark Theme in my program?
by
d7_2_laz
[
Today
at 05:52:40 pm]
IBTable - user defined se...
by
korba812
[
Today
at 04:57:30 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Relational database (Read 647 times)
Pe3s
Sr. Member
Posts: 369
[SOLVED] Relational database
«
on:
November 18, 2022, 05:33:21 pm »
Hello I have this question, I have a SQLite relational database with two tables. A one-to-many relationship. If I delete a record from Table 1 it does not delete records in Table 2. How can I delete the record from table 1 and all records related to the record in table 2 ?
Greetings
«
Last Edit: November 19, 2022, 10:02:43 am by Pe3s
»
Logged
Thaddy
Hero Member
Posts: 12898
Re: Relational database
«
Reply #1 on:
November 18, 2022, 06:53:47 pm »
DELETE <X> FROM <Y> JOIN X1 WHERE <value> or similar. (or a double select)
But in a.o Sqlite you can use:
https://www.techonthenet.com/sqlite/foreign_keys/foreign_delete.php
«
Last Edit: November 18, 2022, 06:57:08 pm by Thaddy
»
Logged
Who is responsable for that!! The caller or the callee.. Out! ya'll, NOW. In UTC time, please, so maybe Yesterday or tomorrow.
paweld
Hero Member
Posts: 621
Re: Relational database
«
Reply #2 on:
November 18, 2022, 06:56:48 pm »
Foregin Keys:
https://www.sqlite.org/foreignkeys.html
Logged
Best regards / Pozdrawiam
paweld
Thaddy
Hero Member
Posts: 12898
Re: Relational database
«
Reply #3 on:
November 18, 2022, 06:57:42 pm »
posts crossed
Logged
Who is responsable for that!! The caller or the callee.. Out! ya'll, NOW. In UTC time, please, so maybe Yesterday or tomorrow.
Pe3s
Sr. Member
Posts: 369
Re: Relational database
«
Reply #4 on:
November 18, 2022, 08:17:18 pm »
Thank you
Logged
Pe3s
Sr. Member
Posts: 369
Relational database
«
Reply #5 on:
November 18, 2022, 08:55:42 pm »
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
Button10Click
(
Sender
:
TObject
)
;
begin
if
MessageDlg
(
'Question'
,
'Delete record'
,
mtWarning
,
mbYesNo
,
0
)
=
mrYes
then
begin
iId
:
=
ZQuery1
.
FieldByName
(
'Category_ID'
)
.
AsInteger
;
ZQuery1
.
SQL
.
Text
:
=
'DELETE x FROM database AS x JOIN database1 AS y ON x.Category_ID = y.Category_ID WHERE Category_ID =: Category_ID'
;
ZQuery1
.
ParamByName
(
'Category_ID'
)
.
AsInteger
:
=
iId
;
ZQuery1
.
ExecSQL
;
ZQuery1
.
SQL
.
Text
:
=
'SELECT * FROM database'
;
ZQuery1
.
Open
;
end
;
end
;
Why is the code not working , where is the error ?
Logged
paweld
Hero Member
Posts: 621
Re: Relational database
«
Reply #6 on:
November 18, 2022, 09:16:25 pm »
You have open dataset - close it. After 5th line add:
Code: Pascal
[Select]
[+]
[-]
ZQuery1
.
Close
;
Logged
Best regards / Pozdrawiam
paweld
Pe3s
Sr. Member
Posts: 369
Re: Relational database
«
Reply #7 on:
November 18, 2022, 09:35:46 pm »
SQL error logic ?
Logged
dseligo
Hero Member
Posts: 953
Re: Relational database
«
Reply #8 on:
November 18, 2022, 09:38:29 pm »
Quote from: Pe3s on November 18, 2022, 08:55:42 pm
Why is the code not working , where is the error ?
If 'database' and 'database1' are your table names then try this:
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
Button10Click
(
Sender
:
TObject
)
;
begin
if
MessageDlg
(
'Question'
,
'Delete record'
,
mtWarning
,
mbYesNo
,
0
)
=
mrYes
then
begin
iId
:
=
ZQuery1
.
FieldByName
(
'Category_ID'
)
.
AsInteger
;
ZQuery1
.
SQL
.
Text
:
=
'DELETE FROM database WHERE Category_ID = :Category_ID'
;
ZQuery1
.
ParamByName
(
'Category_ID'
)
.
AsInteger
:
=
iId
;
ZQuery1
.
ExecSQL
;
ZQuery1
.
SQL
.
Text
:
=
'DELETE FROM database1 WHERE Category_ID = :Category_ID'
;
ZQuery1
.
ParamByName
(
'Category_ID'
)
.
AsInteger
:
=
iId
;
ZQuery1
.
ExecSQL
;
ZQuery1
.
SQL
.
Text
:
=
'SELECT * FROM database'
;
ZQuery1
.
Open
;
end
;
end
;
Logged
Pe3s
Sr. Member
Posts: 369
Re: Relational database
«
Reply #9 on:
November 19, 2022, 10:02:27 am »
Thank you
Logged
Zvoni
Hero Member
Posts: 1662
Re: [SOLVED] Relational database
«
Reply #10 on:
November 19, 2022, 07:09:14 pm »
IIRC, SQLiteconnection turns on foreign_keys PRAGMA to on (think i saw it somewhere deep down in its sourcecode).
That said: if you don‘t create your detail-table with foreign key with option ON DELETE CASCADE, then you get what‘s happening to you.
Logged
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
Databases
»
[SOLVED] Relational database
TinyPortal
© 2005-2018