插入错误,NOT NULL约束失败

时间:2018-09-03 12:14:17

标签: android

按下“收藏夹”按钮后,给我这个错误

logcat

E/SQLiteLog: (1299) abort at 28 in [INSERT INTO StudioMusik(fasilitas,telepon,longitude,gambar,nama,komentar,latitude,informasi,_id,jamlite,alamat,harga) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)]: NOT NULL constraint failed: StudioMusik.
09-03 18:24:36.901 29861-29861/com.skripsi.axioo.percobaan5 E/SQLiteDatabase: Error inserting fasilitas= telepon=(0271) 852590 longitude=110.848583 gambar=8.jpg nama=SMA 8 N SURAKARTA komentar= latitude=-7.546393 informasi= _id=8 jamlite= alamat=Jl. Sumbing VI No.49, Mojosongo, Jebres, Solo harga=
    android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: StudioMusik.ratingalat (code 1299)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)

问题错误>   NOT NULL约束失败:StudioMusik.ratingalat(代码1299)

在我的代码中,该代码永不存在,尝试对其进行重建或清理项目,结果是相同的,该bug始终出现在StudioMusik.ratingalat上,尝试搜索该代码,但是我确定该代码已被删除,使用搜索所有内容且没有找到该代码

源代码

private SQLiteDatabase database;

private DBHelper dbHelper;

private String[] allColumns = { DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME,
        DBHelper.COLUMN_ALAMAT, DBHelper.COLUMN_HARGA, DBHelper.COLUMN_GAMBAR,
        DBHelper.COLUMN_JAMLITE,
        DBHelper.COLUMN_CALLLITE,
        DBHelper.COLUMN_INFORMASI,
        DBHelper.COLUMN_FASILITAS,
        DBHelper.COLUMN_KOMENTAR,
        DBHelper.COLUMN_LATITUDE,
        DBHelper.COLUMN_LONGITUDE
};

public DBDataSource(Context context)
{
    dbHelper = new DBHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

// Insert data
public boolean insertStudioMusik(String id, String nama, String alamat, String harga, String gambar,
                                 String jam,
                                 String telepon,
                                 String informasi,
                                 String fasilitas,
                                 String komentar,
                                 Double latitude,
                                 Double longitude) {

    ContentValues values = new ContentValues();
    values.put(DBHelper.COLUMN_ID, id);
    values.put(DBHelper.COLUMN_NAME, nama);
    values.put(DBHelper.COLUMN_ALAMAT, alamat);
    values.put(DBHelper.COLUMN_HARGA, harga);
    values.put(DBHelper.COLUMN_GAMBAR, gambar);
    values.put(DBHelper.COLUMN_JAMLITE, jam);
    values.put(DBHelper.COLUMN_CALLLITE, telepon);
    values.put(DBHelper.COLUMN_INFORMASI, informasi);
    values.put(DBHelper.COLUMN_FASILITAS, fasilitas);
    values.put(DBHelper.COLUMN_KOMENTAR, komentar);
    values.put(DBHelper.COLUMN_LATITUDE, latitude);
    values.put(DBHelper.COLUMN_LONGITUDE, longitude);

    long insertId = database.insert(DBHelper.TABLE_NAME,null,
            values);

    if (insertId == -1) return false;
    else return true;
}

//set data
private StudioMusik cursorToStudioMusik(Cursor cursor)
{
    StudioMusik studiomusik = new StudioMusik();
    // debug LOGCAT
    studiomusik.setId(cursor.getInt(0));
    studiomusik.setNama(cursor.getString(1));
    studiomusik.setAlamat(cursor.getString(2));
    studiomusik.setHarga(cursor.getString(3));
    studiomusik.setGambar(cursor.getString(4));
    studiomusik.setJam(cursor.getString(5));
    studiomusik.setTelepon(cursor.getString(6));
    studiomusik.setInformasi(cursor.getString(7));
    studiomusik.setFasilitas(cursor.getString(8));
    studiomusik.setKomentar(cursor.getString(9));
    studiomusik.setLatitude(cursor.getString(10));
    studiomusik.setLongitude(cursor.getString(11));

    return studiomusik;
}

//mengambil semua data StudioMusik
public ArrayList<StudioMusik> getStudioMusik() {
    ArrayList<StudioMusik> daftarStudioMusik = new ArrayList<StudioMusik>();

    Cursor cursor = database.query(DBHelper.TABLE_NAME,
            allColumns, null, null, null, null, null, null);

    // pindah ke data paling pertama
    cursor.moveToFirst();
    // jika masih ada data, masukkan data barang ke
    // daftar barang
    while (!cursor.isAfterLast()) {
        StudioMusik studioMusik = cursorToStudioMusik(cursor);
        daftarStudioMusik.add(studioMusik);
        cursor.moveToNext();
    }
    cursor.close();
    return daftarStudioMusik;
}


//ambil satu barang sesuai id
public boolean isFavorite(Integer id)
{
    StudioMusik studiomusik = new StudioMusik();
    //select query
    Cursor cursor = database.query(DBHelper.TABLE_NAME, allColumns, "_id ="+id,null, null, null, null, null);
    if (cursor.getCount()>0) return true;
    else return false;
}

//update studio yang diedit
public void updateStudioMusik(StudioMusik b)
{
    //ambil id barang
    String strFilter = "_id=" + b.getId();
    //memasukkan ke content values
    ContentValues args = new ContentValues();
    //masukkan data sesuai dengan kolom pada database
    args.put(DBHelper.COLUMN_NAME, b.getNama());
    args.put(DBHelper.COLUMN_ALAMAT, b.getAlamat());
    args.put(DBHelper.COLUMN_HARGA, b.getHarga());
    args.put(DBHelper.COLUMN_GAMBAR, b.getGambar());
    args.put(DBHelper.COLUMN_JAMLITE, b.getJam());
    args.put(DBHelper.COLUMN_CALLLITE, b.getTelepon());
    args.put(DBHelper.COLUMN_INFORMASI, b.getInformasi());
    args.put(DBHelper.COLUMN_FASILITAS, b.getFasilitas());
    args.put(DBHelper.COLUMN_KOMENTAR, b.getKomentar());
    args.put(DBHelper.COLUMN_LATITUDE, b.getLatitude());
    args.put(DBHelper.COLUMN_LONGITUDE, b.getLongitude());

    //update query
    database.update(DBHelper.TABLE_NAME, args, strFilter, null);
}

// delete studio sesuai ID
public void deleteStudioMusik(Integer id)
{
    String strFilter = "_id=" + id;
    database.delete(DBHelper.TABLE_NAME, strFilter, null);
}

除了logcat以外,任何人都可以说出真正的问题,谢谢

1 个答案:

答案 0 :(得分:0)

嗯,真正的问题是您的数据库状态仍然与已删除的旧代码相同。

因此,您需要升级数据库以更改表结构。

您可以参考链接upgrade database。如果您仍然有疑问,请发布数据库代码以帮助您升级数据库。

干杯!