Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 错误SQLiteException:外键不匹配_Android_Database_Sqlite_Android Sqlite_Foreign Key Relationship - Fatal编程技术网

Android 错误SQLiteException:外键不匹配

Android 错误SQLiteException:外键不匹配,android,database,sqlite,android-sqlite,foreign-key-relationship,Android,Database,Sqlite,Android Sqlite,Foreign Key Relationship,我想在表中插入一个寄存器,其结构如下: db=this.openOrCreateDatabase("calificaciones.db", MODE_PRIVATE, null); db.execSQL("PRAGMA foreign_keys=ON"); db.execSQL("create table if not exists usuarios (idusuario integer primary key autoincrement, nusuario text, contrasena t

我想在表中插入一个寄存器,其结构如下:

db=this.openOrCreateDatabase("calificaciones.db", MODE_PRIVATE, null);
db.execSQL("PRAGMA foreign_keys=ON");
db.execSQL("create table if not exists usuarios (idusuario integer primary key autoincrement, nusuario text, contrasena text, correo text);");
db.execSQL("create table if not exists alumnos (idalumno integer primary key, apellidos text, nalumno text, especialidad text, grado text, grupo text);");
db.execSQL("create table if not exists materias (idmateria integer primary key, nmateria text,"+" docente text, horas integer);");
db.execSQL("create table if not exists calificacion (idcalif integer primary key autoincrement, idalumno integer, idmateria integer, idusuario integer, calificacion integer, parcial integer, foreign key(idalumno) references alumnos(idalumno), foreign key(idmateria) references materias(idmateria), foreign key(idusuario) references alumnos(idusuario));");
但是,在将寄存器插入表“Calificacion”时,它给了我以下错误消息:

android.database.sqlite.SQLiteException:外键不匹配-“calificacion”引用“alumnos”(代码1):,编译时:插入calificacion(idalumno、IdMateriala、Idusario、calificacion、parcial)值(1533005079040942069,1,10,3)

即使其他表的寄存器与我试图插入到“Calificaciones”中的数据匹配,也会发生这种情况。我试图在SQLFIDLE上使用完全相同的查询重现相同的错误,但在那个平台上,不知何故,它没有导致任何错误

我的语法有什么问题?

来自:

如果数据库架构包含需要查看多个表定义才能识别的外键错误,则在创建表时不会检测到这些错误。相反,此类错误会阻止应用程序准备SQL语句,这些SQL语句以使用外键的方式修改子表或父表的内容

在以下情况下,可能会报告外键DML错误:

  • 父表不存在,或
  • 外键约束中命名的父键列不存在,或
  • 父键列 在外键约束中命名的不是 父表和不受使用 在创建表中指定的排序顺序,或
  • 儿童桌 引用父项的主键,但不指定 主键列和中的主键列数 父项与子键列的数目不匹配
然而,看一看您的代码,似乎大多数代码都被遵循了。是否确定在创建过程中没有错误,例如,无法首先创建父表?(因为您在插入过程中稍后会收到错误,我想知道您是否在插入过程的早期丢失了错误。)

还有,您确定这些是您正在创建的表吗?因为你有一个
if不存在的
子句;可能存在具有不同模式的表的旧版本,而您当前的上述(我认为)正确代码并不是现在实际运行的代码?使用工具从您的设备/仿真器查看或添加原始SQL Lite数据库,以查看其中的架构是否与您当前提供的上述命令匹配。

说明:

在以下情况下可能会报告外键DML错误:[……]

  • 外键约束中命名的父键列不存在
在这种情况下,父键列确实不存在:

... foreign key(idusuario) references alumnos(idusuario)
                                      ▔▔▔▔▔▔▔ ▔▔▔▔▔▔▔▔▔