我的数据库中有一列用于日期。目前我使用miliseconds值作为替代,但我希望能够添加实际的日期和时间。
我已经看到可以从this question使用以下内容,但我不确定如何在我当前的代码中实现它? (如下所示):
Time now = new Time();
now.setToNow();
另外,我是否需要更改DatabaseHelper类,将该列从Long更改为其他值?
添加到数据库的当前代码:
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);
}
}
数据库助手类:
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;
}
}
答案 0 :(得分:1)
使用Calendar c = Calendar.getInstance();
long time= c.getTimeInMillis();
要获得当前时间(以毫秒为单位),您可以将其放入ContentValue以将其插入数据库
values.put(COL_DATE, time);
答案 1 :(得分:1)
通过以下链接,这将对您有帮助,
http://www.sqlite.org/datatype3.html#affinity
无论你插入什么,Sqlite类型的亲和力都会照顾它。如果您的数据类型不匹配,或者在插入时,这将成为表格中的文本值。