SQLite CURRENT_TIME

In SQLite, CURRENT_TIME is a built-in date and time function that returns the current time in the ‘HH:MM:SS’ format. It does not include the date portion, only the time component.

The CURRENT_TIME function is often used when you want to retrieve the current time within a SQL query or as a default value for a column in a table. Here’s a brief explanation and example:

Syntax

CURRENT_TIME

Example

Suppose you have a table named events with columns for event name and event time. You can use CURRENT_TIME as a default value for the event time column to automatically record the time when an event is added:

CREATE TABLE events (
    event_name TEXT,
    event_time TIME DEFAULT CURRENT_TIME
);

In this example, the event_time column is defined with a default value of CURRENT_TIME, which means that if you insert a new row into the events table without specifying a value for event_time, it will automatically be set to the current time.

Inserting a new event:

INSERT INTO events (event_name) VALUES ('Meeting');

In this case, the event_time column will be set to the current time when the INSERT statement is executed.

Retrieving current time in a query:

You can also use CURRENT_TIME directly in a query to retrieve the current time:

SELECT CURRENT_TIME;

This query will return a result set with a single column and a single row containing the current time.

In summary, CURRENT_TIME in SQLite is a useful function for obtaining the current time within SQL queries or setting default values for time-related columns in tables. It simplifies the process of working with time data in your SQLite database.