android - SQLite: Getting value 0 from a query when insertions work correctly -
i have table notes (_id, title, body, category)
, table categories (_id, name)
, referencing category categories._id.
with method create note in sqlite database.
/** * create new note using title , body provided, , associated * category row id provided. if note created * return new rowid note, otherwise return -1 indicate failure. * * @param title title of note * @param body body of note * @param category category associated note * @return rowid or -1 if failed */ public long createnote(string title, string body, string category) { if (title == null || title.equals("") || body == null) { return -1; } else { contentvalues initialvalues = new contentvalues(); initialvalues.put(note_key_title, title); initialvalues.put(note_key_body, body); // if has associated category if (!category.equals("no category")) { cursor idcategory = fetchcategory(category); long catid = idcategory.getlong(idcategory.getcolumnindex("_id")); initialvalues.put(note_key_cat, catid); // else, has no category } else { initialvalues.put(note_key_cat, (byte) null); } return mdb.insert(database_note_table, null, initialvalues); } }
and this, notes of database.
/** * return cursor on list of notes in database * * @param mode - if title, returns list ordered title of notes. * - if category, returns list ordered category of notes. * - returns null in case. * @param category - category of notes return (no category if none) * @return cursor on notes */ public cursor fetchallnotes(string mode, string category) { //// order string order; // mode title if (mode.equals("title")) { order = note_key_title; // mode categories } else if (mode.equals("category")) { order = cat_key_name; // forbidden mode } else { return null; } // no category filter if no category string cat; if (category.equals("no category")) { cat = ""; } else { cat = " " + cat_key_name + "='" + category + "'"; } // left outer because there notes without category string query = "select * " + database_note_table + " left outer join " + database_cat_table + " c on " + note_key_cat + "=c." + cat_key_rowid + cat + " order " + order; return mdb.rawquery(query, null); }
edit:
i realize several
createnote()
operations, , notes correctly inserted (and method returns correct rowid of note created [1, 2, ... ]).i realize from
fetchallnotes()
operation, notes returned. however, notesno category
have rowid attribute 0 (whencreatenote()
returned me positive value). , notes category have positive rowid value, not correct.
for example:
createnote (title: a, body: b, category: no category). returns 1 createnote (title: c, body: d, category: no category). returns 2 createnote (title: e, body: f, category: have category). returns 3
fetchallnotes() returns:
1. note(0, a, b, no category) 2. note(0, c, d, no category) 3. note(1, e, f, have category)`
any idea of happening?
replace *
full field list (which best practice).
return needed fields in cursor.
Comments
Post a Comment