H
H
Hakito2015-12-13 14:33:50
Android
Hakito, 2015-12-13 14:33:50

How to maintain referential integrity in SQLite?

Hello!
I am writing an android application that works with SQLite. I have paintings there, their authors, all that. The connection between the picture and the author is many to one. That is, the picture has an author field that refers to the author's id. If I delete an author who already has paintings, the DBMS does not notify in any way that referential integrity has been violated.
How can this situation be handled? I need to prevent the user from simply deleting an author that is used in the "Pictures" table

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xlusv, 2015-12-13
@hakito

1. Do you have foreign key references assigned? If not, read this documentation section: https://www.sqlite.org/foreignkeys.html
An important point: in SQLite there is no way to add a constraint via ALTER TABLE, i. like this: "ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY key_name REFERENCES ref_table.ref_key_name" as in the standard. You will have to delete and create a table with a new schema, saving the data somewhere if you need it.
2. Check this point: www.sqlite.org/pragma.html#pragma_foreign_keys
By default, the parameter is set to OFF. Needed in ON. Connect to the DB from a terminal or client and change the value.

D
Dimonchik, 2015-12-13
@dimonchik2013

SQLite is not a DBMS,
therefore it needs to be processed as in a DBMS without integrity support - on the application side

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question