What are the Foreign key constraints in Android using SQLite

0 votes

I am working on an Android app that uses SQLite, in which I have two tables: tracks and waypoints. Here, a track can have many waypoints, but a waypoint is assigned to only 1 track.

Waypoints table have a column called "trackidfk" which inserts the track_ID once a track is made, but no Foreign Key constraints are set on this column.

So, what should I do, if I want to delete a track and all the assigned waypoints along with it? Please suggest.

Heres my code to create the waypoints table:

public void onCreate(SQLiteDatabase db) {
    db.execSQL( "CREATE TABLE " + TABLE_NAME 
                + " (" 
                + _ID         + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
                + LONGITUDE   + " INTEGER," 
                + LATITUDE    + " INTEGER," 
                + TIME        + " INTEGER,"
                + TRACK_ID_FK + " INTEGER"
                + " );"
              );

    ...
}
Aug 24, 2018 in Java by v.liyyah
• 1,300 points
60 views

1 answer to this question.

0 votes

You can use, Foreign key constraints with on delete cascade by manually enabling them.
As for me, I just added the following to my SQLOpenHelper, which helped.

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}

Here, I declared my referencing column as:

mailbox_id INTEGER REFERENCES mailboxes ON DELETE CASCADE
answered Aug 24, 2018 by geek.erkami
• 2,320 points

Related Questions In Java

0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,320 points
38 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,540 points
125 views
0 votes
1 answer

To use JavaMail API for sending mails in Android without using the default/built-in App

ADD 3 jars found in the following ...READ MORE

answered Jun 6, 2018 in Java by sridhar
• 160 points
308 views
0 votes
1 answer

What are all the different ways to create an object in Java?

There are four different ways to create ...READ MORE

answered Oct 16, 2018 in Java by Daisy
• 8,040 points
54 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 6,144 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
1,619 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
4,314 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
333 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 8, 2018 in Java by geek.erkami
• 2,320 points
262 views
0 votes
1 answer

Fetch list of in-between dates using Java

I tried generating the list of the ...READ MORE

answered May 10, 2018 in Java by geek.erkami
• 2,320 points
52 views