java - How to multiply SQL columns from 2 tables and insert into a seperate column? -
i trying write sql android application takes quantity of inventory 1 table multiply price of item in table total store column. based on have read seems pretty simple, isn't working me. have tried multiple solutions, @ point have done put sql code in separate method called totalprice() called on button click in method.
public static final string table_invoice = "invoice"; public static final string invoice_id = "invoice_id"; public static final string invoice_date = "date"; public static final string table_inventory = "inventory"; public static final string inventory_id = "inventory_id"; public static final string inventory_item = "item"; public static final string inventory_xs = "extrasmall"; public static final string inventory_s = "small"; public static final string inventory_m = "medium"; public static final string inventory_l = "large"; public static final string inventory_xl = "extralarge"; public static final string inventory_xxl = "extraextralarge"; public static final string inventory_price = "price"; public static final string table_ordered = "ordered"; public static final string ordered_id = "ordered_id"; public static final string ordered_item = "item"; public static final string ordered_size = "size"; public static final string ordered_quantity = "quantity"; public static final string ordered_total = "total"; public dbhelper(context context){ super(context, db_name, null, db_version); } public void oncreate(sqlitedatabase db) { string create_customer_table = "create table " + table_customer + " (" + c_customer_id + " integer primary key, " + c_customer_name + " text, " + c_email + " text, " + c_street_address + " text, " + c_city + " text, " + c_state + " text, " + c_zip + " text, " + c_phone + " text " + ")"; string create_inventory_table = "create table " + table_inventory + " (" + inventory_id + " integer primary key, " + inventory_item + " text, " + inventory_xs + " text, " + inventory_s + " text, " + inventory_m + " text, " + inventory_l + " text, " + inventory_xl + " text, " + inventory_xxl + " text, " + inventory_price + " real " + ")"; string create_invoice_table = "create table " + table_invoice + " (" + invoice_id + " integer primary key, " + invoice_date + " datetime, " + c_customer_id + " integer, " + "foreign key(" + c_customer_id + ") references " + table_customer + " (" + c_customer_id + "));"; string create_ordered_table = "create table " + table_ordered + " (" + ordered_id + " integer primary key, " + ordered_item + " text, " + ordered_size + " text, " + ordered_quantity + " integer, " + ordered_total + " real, " + inventory_id + " integer, " + "foreign key(" + inventory_id + ") references " + table_inventory + " (" + inventory_id + "));"; db.execsql(create_customer_table); db.execsql(create_inventory_table); db.execsql(create_invoice_table); db.execsql(create_ordered_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_customer); //create tables again this.oncreate(db); } public cursor totalprice() { //open database sqlitedatabase db = this.getwritabledatabase(); cursor res = db.rawquery("select " + ordered_quantity + ", " + inventory_price + ", " + ordered_quantity + " * " + inventory_price + " " + ordered_total + " " + table_ordered + " inner join " + table_inventory + " on ordered." + inventory_id + " = inventory." + inventory_id, null); return res; }
i have class calls method within button click.
public class addinvoice extends appcompatactivity { button addinvoice; dbhelper db; edittext date, cid, item, size, invid, quantity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_invoice); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); db = new dbhelper(this); addinvoice = (button) findviewbyid(r.id.btnaddtoinvoice); date = (edittext) findviewbyid(r.id.etdate); cid = (edittext) findviewbyid(r.id.etcid); item = (edittext) findviewbyid(r.id.itemname); size = (edittext) findviewbyid(r.id.etsize); invid = (edittext) findviewbyid(r.id.etaddinventoryid); quantity = (edittext) findviewbyid(r.id.etquantity); addinvoice.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { addinvoice(); addordereditem(); db.totalprice(); date.settext(""); cid.settext(""); item.settext(""); size.settext(""); invid.settext(""); quantity.settext(""); } } ); getsupportactionbar().setdisplayhomeasupenabled(true); } public void addordereditem() { boolean isinserted = db.addordereditem(item.gettext().tostring(), size.gettext().tostring(), integer.parseint(invid.gettext().tostring()), integer.parseint(quantity.gettext().tostring())); db.totalprice(); if (isinserted == true) toast.maketext(addinvoice.this, "item inserted", toast.length_short).show(); else toast.maketext(addinvoice.this, "item not inserted", toast.length_short).show(); } public void addinvoice(){ boolean isinserted = db.addinvoice(date.gettext().tostring(), cid.gettext().tostring()); if (isinserted == true) toast.maketext(addinvoice.this, "invoice inserted", toast.length_short).show(); else toast.maketext(addinvoice.this, "invoice not inserted", toast.length_short).show(); }
}
now when run code value stored in total price column null. help.
i assume wants execute following inner join query:
select column1 * column2 result table1 t1 inner join table2 t2 dt on t1.key = t2.key
and free use value in java code.
Comments
Post a Comment