Jaguar Jaguar - 17 days ago 5
C# Question

Which datatype to use to store TimeTaken field in SQLIte?

I want to create a field in SQLite DB(using C#) to store time taken(duration) field. The value will be in

hr:min:sec
format, like
'10:10:00'
which means 10 hrs, 10 min and 00 seconds. WHich Data type should I use for this? I know
DateTime
will not be adequate since its used to store date-time.

Answer

Use a TimeSpan in C#. This is meant to represent an elapsed duration of time, which is what you are describing.

In SQLite, you should store an INTEGER type so that your data is sortable. You simply need to decide what granularity you need.

For example, you could store the TimeSpan.Ticks if you care about the absolute finest precision possible. You would need an INTEGER of 8 bytes for this. When loading, you can use either TimeSpan.FromTicks, or the constructor new TimeSpan(ticks).

More likely, you will want to store a whole number of seconds, which you can get from TimeSpan.TotalSeconds. You could probably use an INTEGER of 4 bytes for this. When loading, you can use TimeSpan.FromSeconds to recreate the timespan from your integer.

Comments