java - Android Adding DATE (now) to SQLite database -
currently sqlite database working great, have read on various methods such this one achieve this, though having issues setting date constructor , how define data use in putting database (and getting back!)
so far have used static final string define data variables, notsure correct way use date.
i'm not concerned time, date entry created.
below java class:
import java.sql.date; import java.text.simpledateformat; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class stats { public static final string key_rowid = "_id"; public static final string key_weight = "weight"; public static final string key_waist = "waist"; public static final string key_chest = "chest"; public static final string key_legs = "legs"; public static final string key_arms = "arms"; public static final string key_date = "date"; private static final string database_name = "statsdb"; private static final string database_table = "personalstats"; private static final int database_version = 3; private dbhelper ffhelper; private final context ffcontext; private sqlitedatabase ffdatabase; private static class dbhelper extends sqliteopenhelper { public dbhelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_weight + " text not null, " + key_waist + " text not null, " + key_chest + " text not null, " + key_legs + " text not null, " + key_arms + " text not null, " + key_date + " text not null);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + database_table); oncreate(db); } } public stats(context c) { ffcontext = c; } public stats open() throws sqlexception { ffhelper = new dbhelper(ffcontext); ffdatabase = ffhelper.getwritabledatabase(); return this; } public void close() { ffhelper.close(); } public long createentry(string weight, string waist, string chest, string legs, string arms) { simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); date date = new date(); contentvalues cv = new contentvalues(); cv.put(key_weight, weight); cv.put(key_waist, waist); cv.put(key_chest, chest); cv.put(key_legs, legs); cv.put(key_arms, arms); cv.put("date_created", dateformat.format(date)); return ffdatabase.insert(database_table, null, cv); } public string getdata() { string[] columns = new string[] { key_rowid, key_weight, key_waist, key_chest, key_legs, key_arms, key_date }; cursor c = ffdatabase.query(database_table, columns, null, null, null, null, null); string result = ""; int irow = c.getcolumnindex(key_rowid); int iweight = c.getcolumnindex(key_weight); int iwaist = c.getcolumnindex(key_waist); int ichest = c.getcolumnindex(key_chest); int ilegs = c.getcolumnindex(key_legs); int iarms = c.getcolumnindex(key_arms); int idate = c.getcolumnindex(key_date); (c.movetofirst(); !c.isafterlast(); c.movetonext()) { result = result + c.getstring(irow) + " " + c.getstring(iweight) + " " + c.getstring(iwaist) + " " + c.getstring(ichest) + " " + c.getstring(ilegs) + " " + c.getstring(iarms) + " " + c.getstring(idate)+ "\n"; } return result; } }
obviously time date mentioned have attempted add functionality.
you may put date string in database, string must have format. sqlite is:
yyyy-mm-dd yyyy-mm-dd hh:mm yyyy-mm-dd hh:mm:ss yyyy-mm-dd hh:mm:ss.sss yyyy-mm-ddthh:mm yyyy-mm-ddthh:mm:ss yyyy-mm-ddthh:mm:ss.sss hh:mm hh:mm:ss hh:mm:ss.sss
like yoann write can convert date string with:
simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string dateforinsert = dateformat(new gregoriancalendar(2013, gregoriancalendar.may, 5).gettime());
"now" can set with:
string dateforinsert = dateformat(new gregoriancalendar().gettime());
Comments
Post a Comment