简体   繁体   中英

how to store a time value in SQLite database in Android?

I want to store a timevalue in an SQLite database in Android. My time value is in EditText, and when I click the save button I would like the time value to be stored in the database. And also I want to review the already-stored value in the database.

SQLite doesn't have a specific datatype for storing times , and leaves it up to you whether you want to store them as text, integers, or floating-point values, so you can establish whatever convention works best for you.

For your application where you want the time to be editable by the user I'd suggest looking into the DatePicker and TimePicker widgets, so that you don't have to worry about parsing and formatting the time as text, and then the Java Calendar class for converting the data from those into a simple value that you can put in the database (I'd suggest using the getTimeInMillis() method to convert it into a integer).

Sqlite + timestamp = Date and time functions

Sqlite + timestamp + android = Timestamp Class

Other options

  • create a column for each piece of info you wish to store (day, hour, min, second, etc)
  • convert time into long and store that instead. Use java.sql.Time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM