I have a problem with foreign keys when I insert data in the table. Using foreign keys to remove the corresponding records. But when I try to enter the data in the table I get this error:
android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
SQLiteOpenHelper
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
@Override
public void onCreate(SQLiteDatabase db) {
DatabaseHelper.db = db;
StringBuilder builder = new StringBuilder();
builder = new StringBuilder();
builder.append("CREATE TABLE {0} (");
builder.append("{1} INTEGER PRIMARY KEY AUTOINCREMENT,");
builder.append("{2} INTEGER,");
builder.append("{3} INTEGER,");
builder.append("{4} INTEGER DEFAULT 0,");
builder.append("{5} INTEGER DEFAULT 0,");
builder.append("{6} INTEGER DEFAULT 1,");
builder.append("{7} REAL DEFAULT 0,");
builder.append("{8} TEXT,");
builder.append("{9} TEXT,");
builder.append("{10} INTEGER,");
builder.append("FOREIGN KEY({2}) REFERENCES Operatori(_id) ON DELETE CASCADE,");
builder.append("FOREIGN KEY({3}) REFERENCES Clienti(_id) ON DELETE CASCADE,");
builder.append("FOREIGN KEY({4}) REFERENCES Prodotti(_id) ON DELETE CASCADE,");
builder.append("FOREIGN KEY({5}) REFERENCES Prodotti(_id) ON DELETE CASCADE");
builder.append(");");
db.execSQL(MessageFormat.format(builder.toString(),
MovimentiNegozioTable.TABLE_NAME,
MovimentiNegozioTable._ID,
MovimentiNegozioTable.ID_OPERATORE,
MovimentiNegozioTable.ID_CLIENTE,
MovimentiNegozioTable.ID_PRODOTTO,
MovimentiNegozioTable.ID_SERVIZIO,
MovimentiNegozioTable.QUANTITA,
MovimentiNegozioTable.PREZZO_SERVIZIO,
MovimentiNegozioTable.DATA,
MovimentiNegozioTable.NOTE,
MovimentiNegozioTable.INS_N));
a part of code for insert rows
ContentValues cv = new ContentValues();
cv.put(MovimentiNegozioTable.ID_SERVIZIO, u.id_servizio);
cv.put(MovimentiNegozioTable.ID_PRODOTTO, id_prodotto);
cv.put(MovimentiNegozioTable.ID_OPERATORE, id_operatore);
cv.put(MovimentiNegozioTable.QUANTITA, u.quantita);
cv.put(MovimentiNegozioTable.ID_CLIENTE, id_cliente);
cv.put(MovimentiNegozioTable.PREZZO_SERVIZIO, u.totale_servizio);
cv.put(MovimentiNegozioTable.DATA, u.data);
cv.put(MovimentiNegozioTable.NOTE, u.note);
cv.put(MovimentiNegozioTable.INS_N, u.ins_n);
db.insert(MovimentiNegozioTable.TABLE_NAME, null, cv);
Aucun commentaire:
Enregistrer un commentaire