android - Multiple SQLite Tables not working correctly -
i attempting create 2 sqlite tables. 1 holds "todoitem"s , other holds "detailitem"s. when creating them , logging them this:
i/todos: [todoitem [id=1, todoitemname=1], todoitem [id=2,todoitemname=2]] i/detailitems: [todoitem [id=1, todoitemname=["1.1","1.2","1.3"]], todoitem [id=2, todoitemname=[]]]
as may notice, seems both tables contain todoitems 1 supposed holding "detailitem"s. here code of databasehelper:
public class sqlitehelper extends sqliteopenhelper { private static final int database_version = 1; private static final string database_name = "productdb.db"; public static final string table_products = "products"; public static final string table_details = "details"; //common columns public static final string column_id = "_id"; //todoitem columns public static final string column_productname = "productname"; //detailitem columns public static final string column_detailid = "detail_id"; public static final string column_detailname = "detailname"; private static final string todotable = "create table " + table_products + " (" + column_id + " integer primary key autoincrement, " + column_productname + " text " + ");"; private static final string detailtable = "create table " + table_details + " (" + column_detailid + " integer primary key autoincrement, " + column_detailname + " text " + ");"; //we need pass database information along superclass public sqlitehelper(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, database_name, factory, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(todotable); db.execsql(detailtable); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_products); db.execsql("drop table if exists " + table_details); oncreate(db); } //--------todoitem methods-------// //add new row database public void addproduct(todoitem product){ contentvalues values = new contentvalues(); values.put(column_productname, product.gettodoitemname()); sqlitedatabase db = getwritabledatabase(); db.insert(table_products, null, values); db.close(); } //delete product database public void deleteproduct(string productname){ sqlitedatabase db = getwritabledatabase(); db.execsql("delete " + table_products + " " + column_productname + "=\"" + productname + "\";"); } /*public void deleteproduct(long id) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_products, column_id + " = ?", new string[] { string.valueof(id) }); }*/ public list getalltodos() { list todoitems = new linkedlist(); string query = "select * " + table_products; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query, null); // parse results todoitem todoitem = null; if (cursor.movetofirst()) { { todoitem = new todoitem(); todoitem.setid(integer.parseint(cursor.getstring(0))); todoitem.settodoitemname(cursor.getstring(1)); todoitems.add(todoitem); } while (cursor.movetonext()); } return todoitems; } public int updateproduct(todoitem todoitem, string v1) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(column_productname, v1); // updating row int = db.update(table_products, values, column_id + " = ?", new string[] { string.valueof(todoitem.getid()) }); db.close(); return i; } //----------detailitem methods-----------// public void adddetailitemname(detailitem detailitem){ contentvalues values = new contentvalues(); values.put(column_detailname, detailitem.getdetailtext()); sqlitedatabase db = getwritabledatabase(); db.insert(table_details, null, values); db.close(); } public void deletedetailitem(string detailname){ sqlitedatabase db = getwritabledatabase(); db.execsql("delete " + table_details + " " + column_detailname + "=\"" + detailname + "\";"); } public list getalldetailitems() { list detailitems = new linkedlist(); string query = "select * " + table_details; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query, null); // parse results detailitem detailitem = null; if (cursor.movetofirst()) { { detailitem = new detailitem(); detailitem.setid(integer.parseint(cursor.getstring(0))); detailitem.setdetailtext(cursor.getstring(1)); detailitems.add(detailitem); } while (cursor.movetonext()); } return detailitems; } public int updatedetailitem(detailitem detailitem, string v1) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(column_detailname, v1); // updating row int = db.update(table_details, values, column_detailid + " = ?", new string[] { string.valueof(detailitem.getid()) }); db.close(); return i; } }
code detailitem:
public class detailitem { public string _detailtext; private int _id; public detailitem(){} public int getid() { return _id; } public void setid(int id) { this._id = id; } public detailitem(string detailtext){ this._detailtext = detailtext; } public string getdetailtext() { return _detailtext; } public void setdetailtext(string detailtext) { this._detailtext = detailtext; } @override public string tostring() { return "todoitem [id=" + _id + ", todoitemname=" + _detailtext + "]"; }
}
code todoitem:
public class todoitem { private string _todoitemname; private int _id; public todoitem(){} public todoitem(string todoitemname){ this._todoitemname = todoitemname; } public int getid() { return _id; } public void setid(int id) { this._id = id; } public void settodoitemname(string todoitemname) { this._todoitemname = todoitemname; } public string gettodoitemname(){ return _todoitemname; } @override public string tostring() { return "todoitem [id=" + _id + ", todoitemname=" + _todoitemname + "]"; }
}
looks todoitem
, detailitem
classes both reporting todoitem
in tostring()
methods. try differentiating , check see log output accurately reflects change.
Comments
Post a Comment