Android sqlite 串聯刪除更新

來源:互聯網
上載者:User

標籤:android   io   os   ar   java   for   sp   資料   art   

============問題描述============


大概是一個租房管理系統,我要求的是
delete from house where houseName = ? 之後,可以串聯刪除與該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(20) primary key, number int not null);";
db.execSQL(sqlhouse);
String sqlroom = "create table room(houseName char(20),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(18) primary key,name char(20) not null,phoneNumber char(11) not null)";
db.execSQL(sqlrenter);
String sqlcheckindocument = "create table CheckInDocument(Id char(18),houseName char(20),roomNumber int,deposit int not null,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 delete cascade on update cascade,foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)";
db.execSQL(sqlcheckindocument);
String sqlcheckoutdocument = "create table checkOutDocument(houseName char(20),roomNumber char(10),checkOutDate Date not null,amount int,waterConsumption int not null,electricityConsumption int not null,"
+ "primary key (houseName,roomNumber),foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)";
db.execSQL(sqlcheckoutdocument);
String sqlrentingdocument = "create table rentingDocument(houseName char(20),roomNumber char(10),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 room(houseName,roomNumber) on delete cascade on update cascade)";
db.execSQL(sqlrentingdocument);
}

在手機上運行後,我把資料庫匯入電腦的sqlite3,y然後執行刪除語句,完全沒問題。
但是在手機上,我執行刪除houseName之後,發現不能實現串聯刪除,請大神指點!!!!!

============解決方案1============


1.
試一下在開啟資料庫以後執行db.execSQL("PRAGMA foreign_keys=ON");這個語句的作用是開啟外鍵支援。
2.
或者在一個事務裡從各個表刪除記錄。

============解決方案2============


sqlite自從3.6.19之後才支援外鍵,但是android在2.1之前都是用的sqlite3.5,為了相容老版本的,在android裡的sqlite外鍵預設關閉

如果你確定你支援支android2.2以上的應用,那麼你就可以在執行以下db.execSQL("PRAGMA foreign_keys=ON");手動開啟支援

Android sqlite 串聯刪除更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.