Android sqlite Cascade Delete Update

Source: Internet
Author: User

============ Problem Description ============


is probably a rental management system, I ask for IS
Delete from house where Housename =? After that, you can cascade to delete content related to the Housename
This is my tables building in Java:


public void OnCreate (Sqlitedatabase db) {
TODO auto-generated Method Stub
String sqlhouse = "CREATE Table House" (Housename char () primary key, number int not null); ";
Db.execsql (Sqlhouse);
String sqlroom = "CREATE TABLE" (Housename char), roomnumber integer, waterconsumption int not NULL "
+ ", Electricityconsumption int not null,primary key (Housename,roomnumber),"
+ "foreign key (Housename) references House (housename) on DELETE cascade on UPDATE cascade);";
Db.execsql (Sqlroom);
String sqlrenter = "CREATE TABLE renter (Id char) primary key,name char () not null,phonenumber char (one) not null)";
Db.execsql (Sqlrenter);
String sqlcheckindocument = "CREATE Table checkindocument (Id char), Housename char, roomnumber int,deposit int not n  ull,rental int not null,startingdate Date not null,surfing int not null,waterfare float not null,electricityfare float not Null,totalamount float,primary Key (Housename,roomnumber), foreign key (ID) references renter (ID) on the delete cascade on UPD Ate cascade,foreign key (Housename,roomnumber) references (housename,roomnumber) on DELETE cascade on UPDATE cascade) ";
Db.execsql (sqlcheckindocument);
String sqlcheckoutdocument = "CREATE Table checkoutdocument (Housename char), Roomnumber char (Ten), Checkoutdate Date Not null,amount int,waterconsumption int not null,electricityconsumption int. NOT NULL, "
+ "PRIMARY key (Housename,roomnumber), foreign Key (Housename,roomnumber) references (Housename,roomnumber) on Delete CASCADE on UPDATE cascade) ";
Db.execsql (sqlcheckoutdocument);
String sqlrentingdocument = "CREATE Table rentingdocument (Housename char), Roomnumber char (TEN), Waterconsumption int Not null,electricityconsumption int not null,rentingdate Date not null,amount int not NULL, "
+ "PRIMARY key (Housename,roomnumber), foreign Key (Housename,roomnumber) references (Housename,roomnumber) on Delete CASCADE on UPDATE cascade) ";
Db.execsql (sqlrentingdocument);
}

After running on the phone, I import the database into the computer's sqlite3,y and then execute the DELETE statement, no problem at all.
But on the phone, I do delete housename after the discovery cannot implement cascade Delete, please big God pointing!!!!!

============ Solution 1============


1.
Try to execute Db.execsql ("PRAGMA foreign_keys=on") after opening the database; This statement is to open foreign key support.
2.
or delete records from individual tables in a single transaction.

============ Solution 2============


SQLite has been supporting foreign keys since 3.6.19, but Android is used before 2.1 sqlite3.5, in order to be compatible with the old version, the SQLite foreign key on Android is turned off by default

If you are sure that you support more than android2.2 applications, then you can perform the following db.execsql ("PRAGMA foreign_keys=on"); Open support manually

Android sqlite Cascade Delete Update

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.