jeudi 24 mars 2016

Can not get data from SQLite database

I've managed to insert data in SQLiteDatabase using this code

    void addAlarm(Alarms alarms) {
        Log.i("Point","process is starting");
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_ALARM, alarms.getAlarm());

        // Inserting Row
        db.insert(TABLE_ALARMS, null, values);
        db.close(); // Closing database connection
        Log.i("Point","inserting is done in addAlarm method");
    }

But then I want to get data using this method:

    Long getAlarm(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_ALARMS, new String[]{COLUMN_ID,
                        COLUMN_ALARM}, COLUMN_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);
        Long l = null;
        if (cursor != null && cursor.moveToFirst()) {
           l = cursor.getLong(1);
        }
        // return contact
        return l;
    }

And after I want to know that the data is still in the database, I use this code:

mySQLiteHelper = new MySQLiteHelper(context);
        Log.i("Point",String.valueOf(mySQLiteHelper.getAlarm(0)));
        Log.i("Point",String.valueOf(mySQLiteHelper.getAlarm(1)));

And at that point I got Point : null at logcat. Can somebody explain why this is going on and what should I do with this???

And also when I want to delete it I get no errors or exceptions. This is code for deleting data:

public void deleteAlarm(int id) {
        Log.i("Point","deleting is starting");
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_ALARMS, COLUMN_ID + " = ?",
                new String[] { String.valueOf(id) });
        db.close();
        Log.i("Point","deleting is over");
    }

Thank you.

Aucun commentaire:

Enregistrer un commentaire