Reputation: 81
I have a column in my database for date. Currently I am using a miliseconds value as a substitute, however I want to be able to add the actual date and time to it.
I have seen that the following can be used from this question but im unsure how to implement it in my current code? (shown below):
Time now = new Time();
now.setToNow();
Also, do I need to change my DatabaseHelper class, to change that column from a Long to a different value?
Current code for adding to the database:
DatabaseHelper db = new DatabaseHelper(this);
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addScore(new Score(UserName.getUserName(), averageMedLevel, System
.currentTimeMillis()));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Score> scores = db.getAllScores();
for (Score s : scores) {
String log = "Name: " + s.getName() + " Meditation: "
+ s.getMeditation() + "Date: " + s.getDate();
// Writing Contacts to log
Log.d("Details: ", log);
}
}
Database helper class:
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private final static String DATABASE_NAME = "MeditationDatabase";
// Contacts table name
private static final String TABLE_SCORE = "scores";
// Contacts Table Columns names
private static final String COL_NAME = "name";
private static final String COL_MED = "meditation";
private static final String COL_DATE = "date";
/**
* Constructor
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* Method that creates the database
*/
@Override
public void onCreate(SQLiteDatabase db) {
// NOTE: may need to alter the below to take out everything after
// INTEGER
String CREATE_TABLE_SCORE = "CREATE TABLE " + TABLE_SCORE + "("
+ COL_NAME + " STRING PRIMARY KEY," + COL_MED + " INTEGER,"
+ COL_DATE + " LONG" + ")";
db.execSQL(CREATE_TABLE_SCORE);
}
/**
* Method that upgrades the database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);
// Create tables again
onCreate(db);
}
/**
* All CRUD operations
*/
// Adding new score details (Name, score, date)
void addScore(Score score) {
SQLiteDatabase db = this.getWritableDatabase();
// ContentValues- holds the values.
ContentValues values = new ContentValues();
values.put(COL_NAME, score.getName());
values.put(COL_MED, score.getMeditation());
values.put(COL_DATE, score.getDate());
// Inserting Row (i.e. the values that were entered from above
db.insert(TABLE_SCORE, null, values);
db.close(); // Closing database connection
}
/**
* Method will return a single Name and score
*
* @param id
* @return
*/
// Getting single contact
Score getScore(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SCORE, new String[] { COL_NAME,
COL_MED, COL_DATE }, COL_NAME + "=?",
new String[] { String.valueOf(name) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Score score = new Score(cursor.getString(0), Integer.parseInt(cursor
.getString(1)), cursor.getLong(2));
// return contact
return score;
}
/**
* Method will return a list of all the scores
*
* @return
*/
// Getting All scores
public List<Score> getAllScores() {
List<Score> scoreList = new ArrayList<Score>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_SCORE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Score score = new Score();
score.setName(cursor.getString(0));
score.setMeditation(Integer.parseInt(cursor.getString(1)));
score.setDate(cursor.getLong(2));
// Adding contact to list
scoreList.add(score);
} while (cursor.moveToNext());
}
// return contact list
return scoreList;
}
}
Upvotes: 0
Views: 113
Reputation: 3221
Go through the following link , this will help you,
http://www.sqlite.org/datatype3.html#affinity
Whatever you insert , Sqlite type affinity will take care of it. If your data type is mismatching or anything this will become a text value in your table while inserting.
Upvotes: 1
Reputation: 4112
Use Calendar c = Calendar.getInstance();
long time= c.getTimeInMillis();
to get current time in milliseconds then u can put this in to ContentValue to insert it in Database
values.put(COL_DATE, time);
Upvotes: 1