java - How to insert my first and subsequent rows into a my database for android project -
i have databasehelper
class builds tables , handles queries, etc.
however, 1 thing stumps me.
i have checklist
table following columns:
checklist_id, int, primary key start_time, text end_time, text
this "insertchecklist()" method, inserts row table:
public long insertchecklist(checklist checklist){ // reference of checklistdb database sqlitedatabase db = this.getwritabledatabase(); // make values inserted contentvalues values = new contentvalues(); values.put(checklistcontract._id, checklist.getid()); values.put(checklistcontract.column_name_start_time, checklist.getstarttime()); values.put(checklistcontract.column_name_end_time, checklist.getendtime()); // insert new row, returning primary key value of new row long newrowid; newrowid = db.insert( checklistcontract.table_name, null, //column_name_nullable (set null not insert new row there no values) values); // close database transaction db.close(); return newrowid; }
so utilize function follows:
- i create
checklist
object - i pass
insertchecklist()
method
however, how know make id in checklist object?
checklist checklist = new checklist(id??, starttime, endtime); // "id" needs equal last id in table, plus 1 insertchecklist(checklist);
does have experience sqlite databases me this? approaching wrong?
the insert()
function returns rowid
of new row.
if table has autoincrementing id, i.e., integer primary key or integer primary key autoincrement column, same rowid
.
in case, automatically assign new value, not put id value contentvalues
, , set id of object value returned insert()
:
public void insertchecklist(checklist checklist) { sqlitedatabase db = this.getwritabledatabase(); try { contentvalues values = new contentvalues(); values.put(checklistcontract.column_name_start_time, checklist.getstarttime()); values.put(checklistcontract.column_name_end_time, checklist.getendtime()); long id; id = db.insertorthrow(checklistcontract.table_name, null, values); checklist.setid(id); } { db.close(); } }
Comments
Post a Comment