Simple SQLiteDatabase usage in android -
Simple SQLiteDatabase usage in android -
my purpose show lastly key_cash value in table_cb on mainactivity
the mainactivity part work fine if not related getcash() method.
could please tell me problems in getcash() method?
this first time using sqlite, hard find answer.
//table_cb has 2 columns (long)key_id , (integer)key_cash //cashbalancetable class description of database move info bundle com.example.money; public class cashbalancetable { private int id; private int cash; public cashbalancetable() {} public cashbalancetable(int id, int cash) { this.id = id; this.cash = cash; } public cashbalancetable(int cash) { this.cash = cash; } public void setcash(int cash) { this.cash = cash; } public int getid() { homecoming id; } public int getcash() { homecoming cash; } } //main activity public class mainactivity extends actionbaractivity { textview textview1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview1 = (textview)findviewbyid(r.id.textview1); databasehelper dbhelper = new databasehelper(this); cashbalancetable cb = new cashbalancetable(); cb = dbhelper.getcash(); textview1.settext("cash : " + cb.getcash() + " won"); //////////////////////////////////////////////////////////// //in databasehelper getcash() method public cashbalancetable getcash() { sqlitedatabase db = this.getreadabledatabase(); string[] columns = new string[] { key_id, key_cash }; cursor cursor = db.query(table_cb, columns, "cash=?", null, null, null, null); cursor.movetolast(); int cash = cursor.getint(cursor.getcolumnindex(key_cash)); cashbalancetable cb = new cashbalancetable(cash); homecoming cb; }
//databasehelper public class databasehelper extends sqliteopenhelper {
//database version private static final int database_version = 1; //database name private static final string database_name = "app_database"; //table names private static final string table_cb = "cashbalancetable"; //common column names private static final string key_id = "id"; private static final string key_cash = "cash"; //cashbalancetable creation statement private static final string create_table_cb = "create table " + table_cb + " (" + key_id + " integer primary key autoincrement, " + key_cash + " integer );"; //ingore on left;; private context context; //constructor public databasehelper(context context) { super(context, database_name, null, database_version); this.context = context; } @override public void oncreate(sqlitedatabase db) { seek { // creating required tables db.execsql(create_table_ce); db.execsql(create_table_ca); db.execsql(create_table_ae); db.execsql(create_table_aa); db.execsql(create_table_cb); db.execsql(create_table_ai); } grab (sqlexception e) { message.message(context, ""+e); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { seek { //on upgrade drop older tables db.execsql("drop table if exists " + table_ce); db.execsql("drop table if exists " + table_ca); db.execsql("drop table if exists " + table_ae); db.execsql("drop table if exists " + table_aa); db.execsql("drop table if exists " + table_cb); db.execsql("drop table if exists " + table_ai); } grab (exception e) { message.message(context, ""+e); } //create new tables oncreate(db); } //insert initial cash value cashbalancetable (input class) public long insertcash(cashbalancetable cb) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_cash, cb.getcash()); //insert row long id = db.insert(table_cb, null, values); homecoming id; } public cashbalancetable getcash() { sqlitedatabase db = this.getreadabledatabase(); string[] columns = new string[] { key_id, key_cash }; cursor cursor = db.query(table_cb, columns, "cash=?", null, null, null, null); cursor.movetolast(); int cash = cursor.getint(cursor.getcolumnindex(key_cash)); cashbalancetable cb = new cashbalancetable(cash); homecoming cb; } }
i think, haven't pass selectionargs in method, please revise code.
public cashbalancetable getcash() { sqlitedatabase db = this.getreadabledatabase(); string[] columns = new string[] { key_id, key_cash }; cursor cursor = db.query(table_cb, columns, "cash=?", new string[]{selectionargs }, null, null, null); cursor.movetolast(); int cash = cursor.getint(cursor.getcolumnindex(key_cash)); cashbalancetable cb = new cashbalancetable(cash); homecoming cb; }
android
Comments
Post a Comment