Android sqlite 级联删除更新

============问题描述============

大概是一个租房管理系统,我要求的是

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");手动打开支持

时间: 2024-12-21 00:44:04

Android sqlite 级联删除更新的相关文章

Sqlite 级联删除

Sqlite设置级联删除(如图) 设置好后,在数据库管理软件中,手动删除,是管用的.但是使用这样的代码删除却无效  :        string sql = string.Format("Delete From TeamInfo Where TeamName = '{0}'", teamName); 需要改成:                string sql = string.Format("PRAGMA foreign_keys=ON; Delete From Tea

mysql级联删除更新

首先,目前在产品环境可用的MySQL版本(指4.0.x和4.1.x)中,只有InnoDB引擎才允许使用外键,所以,我们的数据表必须使用InnoDB引擎. 下面,我们先创建以下测试用数据库表: CREATE TABLE `roottb` (   `id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL,   `data` VARCHAR(100) NOT NULL DEFAULT '',   PRIMARY KEY (`id`) ) TYPE=InnoDB; CR

android sqlite 递归删除一棵子树

背景:android studio 3.0 GreenDao 目标:在android 中,如何做到递归删除某颗子树?? ============================================================= 原文地址:https://www.cnblogs.com/ssqhan/p/8371183.html

仅作记录,游标,级联删除,获取所有该外键的表名

以下内容只是部分原创,仅是之前收集到的知识.只做记录避免遗忘,具体出处已遗忘. 一.游标结构 总是遗忘游标的结构 declare PeriodDataCursor cursor for select ID , ClassTime from @temptable order by [ClassTime] desc open PeriodDataCursor fetch next from PeriodDataCursor into @ID , @firstClsTime while(@@FETCH

MySQL中利用外键实现级联删除、更新

MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定在删除.更新父表时,对子表进行的相应操作,包括RESTRICT.NO ACTION.SET NULL和CASCADE.其中RESTRICT和NO ACTION相同,是指在子表有关联记录的情况下父表不能更新:CASCADE表示父表在更新或者删除时,更新或者删除子表对应记录:SET NULL则是表示父表在更新或者删除的时候,子表的对应字段被S

关于数据的级联删除和更新

两张表:“ProductCategory”,“Product”. 有一个需求是这样的:在删除某个ProductCategory 的时候,同时删除该Category的products. 在MySQL中利用外键实现级联删除 创建以下测试用数据库表: CREATE TABLE `roottb` ( `id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL, `data` VARCHAR(100) NOT NULL DEFAULT '', PRIMARY KEY (`i

数据库事务测试以及级联更新级联删除

数据库事务 start transaction; #开始事务 insert into gzb(gz)values(5000); insert into gzb(gz)values(6000); insert into gzb(gz)values(7000); insert into gzb(gz)values(8000); /*执行事务,并查看是否添加成功数据*/ commit; # commit数据,查看表中数据是否提交 rollback; #数据库回滚 级联更新,级联删除 主要通过两种方式

android listview级联三菜单选择地区,本地数据库sqlite级联地区,item选中不变色

前言:因为找了N多网上的资源都没有好的解决方案,别人都是只给思路没给具体源码,真TMD纠结,干嘛求别人,自己动手才是真,最痛恨那些所谓大牛的作风,给了点点代码就让别人去想,你让我们这种小白情何于堪!!!!!!此例是基于listview来实现本地sqlite实现的! 二话不说,程序猿求的是有图有真相有源码!大家下载后有什么问题可以找到本人:QQ508181017 核心代码如下 1.数据库操作类 package com.icq.demo.db; import java.util.ArrayList;

Oracle外键级联删除和级联更新

1 级联删除 Oracle在外键的删除上有NO ACTION(类似RESTRICT).CASCADE和SET NULL三种行为. 下面以学生-班级为例说明不同情况下的外键删除,学生属于班级,班级的主键是学生的外键. -- 班级表 CRATE TABLE TB_CLASS ( ID NUMBER NOT NULL, --班级主键 NAME VARCHAR2(50), --班级名称 CONSTRAINT PK_TB_CLASS PRIMARY KEY (ID) ); -- 学生表 CREATE TA