Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Databases
»
[SOLVED] Relational database
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
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
File not open??? Why?
by
OC DelGuy
[
Today
at 08:38:51 am]
Controls inside the form ...
by
GetMem
[
Today
at 08:11:31 am]
BUG: OnExit and ComboBox
by
KodeZwerg
[
Today
at 08:10:46 am]
Faster responding Code Ex...
by
KodeZwerg
[
Today
at 07:00:19 am]
A few demos on how to use...
by
Thaddy
[
Today
at 06:47:29 am]
CheckComboBox Item Width
by
seany
[
Today
at 04:46:37 am]
Menu Bar Text Color
by
seany
[
Today
at 04:22:28 am]
Links in forum posts
by
440bx
[
Today
at 03:05:40 am]
DBCheckbox show some text...
by
eldonfsr
[
Today
at 03:04:00 am]
Is there a function calle...
by
OC DelGuy
[
Today
at 01:55:20 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Relational database (Read 574 times)
Pe3s
Sr. Member
Posts: 326
[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: 12585
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
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.
paweld
Hero Member
Posts: 546
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: 12585
Re: Relational database
«
Reply #3 on:
November 18, 2022, 06:57:42 pm »
posts crossed
Logged
Writing code in an overly complex way is usually merely a complete misunderstanding of the goal you are trying to achieve.
Pe3s
Sr. Member
Posts: 326
Re: Relational database
«
Reply #4 on:
November 18, 2022, 08:17:18 pm »
Thank you
Logged
Pe3s
Sr. Member
Posts: 326
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: 546
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: 326
Re: Relational database
«
Reply #7 on:
November 18, 2022, 09:35:46 pm »
SQL error logic ?
Logged
dseligo
Hero Member
Posts: 876
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: 326
Re: Relational database
«
Reply #9 on:
November 19, 2022, 10:02:27 am »
Thank you
Logged
Zvoni
Hero Member
Posts: 1591
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