1

我想将数据插入数据库,但由于外键约束失败,我的应用程序被强制关闭。我已经尝试修复,但我无法解决该错误。

dbHelper.java

public class dbHelper {
private static final String CREATE_tbKontak = "create table "+ tbKontak 
        +"("+ k_id +" integer primary key autoincrement, "+ k_nama 
        +" text, "+ k_nomor +" text)";

private static final String CREATE_tbEnkrip = "create table "+ tbEnkrip 
        +"("+ e_idenkrip +" integer primary key autoincrement, "+ e_chiperteks 
        +" text, "+ e_kunci +" text)";

private static final String CREATE_tbPesan = "create table "+ tbPesan 
        +"("+ p_idpesan +" integer primary key autoincrement, "
        + p_idenkrip +" integer REFERENCES "+ tbEnkrip +"("+ e_idenkrip +") ON UPDATE CASCADE ON DELETE CASCADE, "
        + p_idkontak +" integer REFERENCES "+ tbKontak +"("+ k_id +") ON UPDATE CASCADE ON DELETE CASCADE, " 
        + p_plainteks +" text)";

private static final String CREATE_tbPesanMasuk = "create table "+ tbPesanMasuk 
        +"("+ m_id +" integer primary key autoincrement, "+ m_idkontak +" integer REFERENCES "
        + tbKontak +"("+ k_id +") ON UPDATE CASCADE, "
        + m_chiperteks +" text , "
        + m_waktu +" not null default CURRENT_TIME)";

private static final String CREATE_tbPesanKeluar = "create table "+ tbPesanKeluar 
        +"("+ kel_id +" integer primary key autoincrement, "
        + kel_idpesan +" integer REFERENCES "+ tbPesan +"("+ p_idpesan +") ON UPDATE CASCADE, "
        + kel_waktu +" not null default CURRENT_TIMESTAMP)";

private final Context context;
private DatabaseOpenHelper dbHelper;
private SQLiteDatabase dba;

public dbHelper( Context ctx ) {
    this.context = ctx;
    dbHelper = new DatabaseOpenHelper(ctx);
    dba = dbHelper.getWritableDatabase();
}

private static class DatabaseOpenHelper extends SQLiteOpenHelper {
    public DatabaseOpenHelper( Context context ) {
        super( context, namaDatabase, null, versiDB);   
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_tbKontak);
        db.execSQL(CREATE_tbEnkrip);
        db.execSQL(CREATE_tbPesan);
        db.execSQL(CREATE_tbPesanMasuk);
        db.execSQL(CREATE_tbPesanKeluar);           
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP IF TABLE EXIST "+ tbPesanKeluar);
        onCreate(db);           
    }

    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if(!db.isReadOnly()) {
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }
}

public void inputKontak( String nama, String nomor) {
    dba.execSQL(" INSERT INTO tbKontak ( nama, nomor ) VALUES ( '"+nama+"', '"+nomor+"')");
}

public void inputEnkripsi( String chiperteks, String kunci ) {
    dba.execSQL("INSERT INTO tbEnkrip ( e_chiperteks, e_kunci ) VALUES ( '"+chiperteks+"', '"+kunci+"')");
}

public void inputPesan( String idenkrip, String idkontak, String plainteks ) {
    dba.execSQL("INSERT INTO TB_pesan ( p_idenkrip, p_idkontak, plainteks )VALUES ( '"+idenkrip+"', '"+idkontak+"', '"+plainteks+"' )");
}

public void inputPesanKeluar( String idpesan ) {
    dba.execSQL("INSERT INTO tbPesanKeluar ( kel_idpes ) VALUES ('"+idpesan+"')");
}

public Cursor pilihKontak( String nomor ) {
    Cursor c = dba.rawQuery("SELECT k_id FROM tbKontak where nomor = '"+nomor+"'", null);
    return c;
}

public Cursor pilihEnkripsi( String chiperteks ) {
    Cursor c = dba.rawQuery("SELECT e_idenkrip FROM tbEnkripsi where nomor = '"+chiperteks+"'", null);
    return c;
}

public Cursor pilihPesan( String plainteks ) {
    Cursor c = dba.rawQuery("SELECT p_idpesan FROM tbPesan where nomor = '"+plainteks+"'", null);
    return c;
}   

}

kirimpesan.java

public class KirimPesan extends Activity {
TextView pesane;
TextView namap;
String pesan;
String kunci;
String ciper;
String nama;
String nomor;
String waktu;
String idkontak;
String idenkripsi;
String idpesan;
SQLiteDatabase db;
dbHelper data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.kirimtulispesan);

    data = new dbHelper(this);

    pesane = (TextView)findViewById(R.id.pesane);
    namap = (TextView)findViewById(R.id.namap);

    Bundle paket = getIntent().getExtras();
    pesan = paket.getString("pesan");
    kunci = paket.getString("kunci");
    ciper = paket.getString("ciper");
    nama = paket.getString("nama");
    nomor = paket.getString("nomor");
    namap.setText(nama);
    pesane.setText(ciper);      

    data.inputKontak(nama, nomor);
    data.inputEnkripsi(ciper, kunci);
    data.inputPesan(idenkripsi, idkontak, pesan); //my line error
    data.inputPesanKeluar(idpesan);             
}

public void ambilKontak() {
    Cursor cursorKontak = data.pilihKontak(nomor);
    idkontak = cursorKontak.toString();     
}

public void ambilEnkripsi() {
    Cursor cursorEnkripsi = data.pilihEnkripsi(ciper);
    idenkripsi = cursorEnkripsi.toString();
}

public void ambilPesan() {
    Cursor cursorPesan = data.pilihPesan(pesan);
    }

}

这是我的错误日志猫

Caused by: android.database.sqlite.SQLiteConstraintException: foreign key constraint failed: INSERT INTO TB_pesan ( fk_idenkripsi, fk_idkontak, plainteks )VALUES ( 'null', 'null', 'ndks' )

有人能帮帮我吗。我真的需要解决这个问题。我试图在互联网上搜索,但我的申请没有得到答案

4

2 回答 2

0

错误信息显示insert中使用的前两个值为null,而这两个值是外键,建议你看一下使用的变量,它们没有取值。

于 2013-05-03T12:20:03.703 回答
0

上次我检查:SQLite 要求所有表中的主键字段都命名为“_id”。

于 2013-05-03T13:15:12.223 回答