dimanche 13 décembre 2015

ContentValues does not insert values into table

I have this:

    public void addNewNote(Note n) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_TITLE, n.getNote_title());
    values.put(COLUMN_TEXT, n.getNote_text());
    values.put(COLUMN_FK, n.getNote_ForeignKey()); //this column is integer type
    values.put(COLUMN_PT, String.valueOf(n.getNote_PersonType()));  //this column is char type

    SQLiteDatabase db = this.getWritableDatabase();
    db.insert("tbl_Notes",null, values);
}

as my add method, as I traced it, the record is added successfully. but when I try to get my note with below code, findNote always returns null and cnt is always 0.

public Note findNote(int note_id){
    String query = "select * from tbl_Notes";  // where " + COLUMN_ID + " = " + note_id;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    int cnt = cursor.getCount();
    Note N = new Note();
    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        N.setNote_title(cursor.getString(1));                           //title column
        N.setNote_text(cursor.getString(2));                            //text body column
        N.setNote_ForeignKey(Integer.parseInt(cursor.getString(3)));    //foreign key column
        N.setNote_PersonType(cursor.getString(4).charAt(0));            //person type column
        cursor.close();
    }else {
        N = null;
    }
    return N;
}

and here is my onClick which fires addNewNote:

Note myNote = new Note(
   txt_Title.getText().toString(),
   foreignKey,
   personType,
   txt_Body.getText().toString()
);
tbl_notes.addNewNote(myNote);

what am I doing wrong?!

Aucun commentaire:

Enregistrer un commentaire