Issue
I have a SQLite database in my Andoid App. I have three rows, ID, DATA and DATE, where DATE is filled by SQLite by using the TIMESTAMP
as follows CREATE TABLE $TABLE ($ID INTEGER PRIMARY KEY AUTOINCREMENT, $DATE TEXT NOT NULL, $DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
.
It works fine for me. But when I update the DATA
row, the timestamp does not update with. I update the DATA
ad follows:
fun updateData(id: String, data: String): Boolean {
db.update(TABLE, ContentValues().apply {
put(DATA, data)
}, "$ID = ?", arrayOf(id))
return true
}
After update, the timestamp stays the same. How can I update the timesamp after each DATA updating?
Solution
I fixed the update by changing
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE ($ID INTEGER PRIMARY KEY AUTOINCREMENT, $DATA TEXT, $DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP);")
}
to
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE ($ID INTEGER PRIMARY KEY AUTOINCREMENT, $DATA TEXT, $DATE TEXT;")
}
and then I defined a variable to pick up the current time
private var date = SimpleDateFormat("dd.MM.yyyy hh:mm").format(Date())
and added it to update
mathod:
fun updateData(id: String, data: String): Boolean {
db.update(TABLE, ContentValues().apply {
put(DATA, data)
put(DATE, date)
}, "$ID = ?", arrayOf(id))
return true
}
That's all. Now it updates the date for each change/modification.
Answered By - Mark Delphi
Answer Checked By - Marie Seifert (JavaFixing Admin)