並提前致謝! 當我試圖插入一個具有外鍵的表格時,我會發生一個錯誤。值存在,是相同的類型,並非非零......更有趣的是,第一次失敗,只有第一次!其他插頁完美地工作! 我試圖刪除插入並重復,並且錯誤不再被拋出......只有第一次! 我在Android 2.2(Froyo)上嘗試它,之前是在2.1沒有外鍵。SQLite錯誤android:外鍵不匹配插入數據庫
對不起,這個大「消息」,但我迷路了!
錯誤:
03-30 16:59:56.333: E/AndroidRuntime(28007): android.database.sqlite.SQLiteException: foreign key mismatch: , while compiling: INSERT INTO productoslista(idProducto, idUnidad, comprado, idLista, comentario, cantidad) VALUES(?, ?, ?, ?, ?, ?);
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
03-30 16:59:56.333: E/AndroidRuntime(28007): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1432)
表:
CREATE TABLE [ProductosLista] (
[idLista] INTEGER NOT NULL,
[idProducto] INTEGER NOT NULL,
[cantidad] INTEGER NOT NULL,
[idUnidad] INTEGER NOT NULL,
[comprado] INTEGER NOT NULL,
[comentario] TEXT,
PRIMARY KEY ([idLista],[idProducto],[comentario]),
FOREIGN KEY (idLista) REFERENCES Listas(idLista),
FOREIGN KEY (idProducto) REFERENCES Productos(idProducto),
FOREIGN KEY (idUnidad) REFERENCES Unidades(idUnidad)
CREATE TABLE [Listas] (
[idLista] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[descripcion] TEXT UNIQUE NOT NULL,
[fechaCreacion] INTEGER NOT NULL,
[visible] TEXT NOT NULL,
[estadoCompra] INTEGER NOT NULL
);
CREATE TABLE [Unidades] (
[idUnidad] INTEGER NOT NULL,
[idIdioma] INTEGER NOT NULL,
[abreviatura] TEXT NOT NULL,
[descripcion] TEXT NOT NULL,
--PRIMARY KEY ([idUnidad],[idIdioma]),
FOREIGN KEY (idIdioma) REFERENCES Idiomas(idIdioma)
);
CREATE TABLE [Productos] (
[idProducto] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[idPadre] INTEGER NOT NULL,
[idCategoria] INTEGER NOT NULL,
[idTipoAlimentacion] INTEGER NOT NULL,
[eliminado] INTEGER NOT NULL,
[imagen] INTEGER,
FOREIGN KEY (idPadre) REFERENCES Productos(idProducto),
FOREIGN KEY (idCategoria) REFERENCES Categorias(idCategoria)
);
爲什麼'FOREIGN KEY(idPadre)REFERENCES Productos(idProducto),'CREATE TABLE [Productos]'內''? – GAMA 2012-04-24 11:18:42
我發現了這個問題,很抱歉爲時已晚!我用新的答案編寫了它。 – user675319 2012-06-21 17:45:13
接受你自己的回答... – GAMA 2012-06-22 04:23:48