public void OnCreate (Sqlitedatabase db)
{
//TODO auto-generated method stub
String classessql = ' CREATE tabl E classes (class_id varchar () primary key, "+
" class_name varchar) ";
String studentssql = "CREATE TABLE students" (student_id varchar primary key, "+
" student_name varchar (), score varchar (4), class_id varchar (a), "+
" foreign Key (class_id) references classes (class_id) "+
" on DELETE cascade ON UPDATE cascade) ";
Db.execsql (classessql);
LOG.D ("My", "CREATE TABLE classes:" +classessql);
Db.execsql (studentssql);
LOG.D ("My", "CREATE TABLE Students:" +studentssql);
}
When creating a table, there is the creation of a foreign key, cascading updates and cascading deletions, but when a class is deleted, the students who first belong to the class are not deleted, which means
On DELETE CASCADE on UPDATE cascade
It's expired.
After the information is known: SQLite in the 3.6.19 version to support foreign key constraints, but in order to compatible with the previous program, the default does not enable the feature, if you want to enable the feature each time you need to use the following statement: PRAGMA Foreign_keys = on to open.
That is, you need to perform db.execsql ("PRAGMA foreign_keys=on") when executing a statement that deletes a class.
/**
* Delete a class
* will also delete students students in the class
* @param class_id
/public
void Deleteclass (String class_id)
{
Sqlitedatabase localsqlitedatabase = This.dbhelper.getWritableDatabase ();
Cascade Delete and Cascade update
//////When cascading statements are executed you must first set "PRAGMA foreign_keys=on"
//otherwise the Cascade relationship defaults
Localsqlitedatabase.execsql ("PRAGMA foreign_keys=on");
object[] Arrayofobject = new object[1];
Arrayofobject[0] =class_id;
Localsqlitedatabase.execsql ("Delete from classes where class_id=?", Arrayofobject);
Localsqlitedatabase.close ();
}