android - Getting error on inserting data on table using SQLite -
i'm building android mobile app , use sqlite database. i'm having trouble inserting record on table , returns toast message "insert failed" meaning saving data unsuccessful.
public class databasehelper extends sqliteopenhelper { public static final string database_name = "mobilebuddy.db"; public static final string table_name = "bets_table"; public static final string col_1 = "combination"; public static final string col_2 = "draw_date"; public static final string col_3 = "trans_date"; public databasehelper(context context) { super(context, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + "(" + col_1 + " text, " + col_2 + " datetime, " + col_3 + " datetime)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } public boolean insertbetdata(string combination, string drawdate, string transdate) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col_1, combination); contentvalues.put(col_2, drawdate); contentvalues.put(col_3, transdate); long result = db.insert(table_name, null, contentvalues); if (result == 1) return true; else return false; } }
i used code below in onclick event , should trigger insertion of data on table.
calendar = calendar.getinstance(); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); boolean issave = lottodb.insertbetdata(validbet, sdf.format(calendar.gettime()), sdf.format(calendar.gettime())); if (issave == true) toast.maketext(playsixnumbers.this, "inserted", toast.length_short).show(); else toast.maketext(playsixnumbers.this, "insert failed", toast.length_short).show();
what's wrong codes? hope understand inquiry..
the documentation says insert()
function returns
the row id of newly inserted row, or -1 if error occurred
the id not 1
. have check return value not error value.
and better idea use insertorthrow()
instead useful error message.
Comments
Post a Comment