sqliteHow do I store a timestamp in an SQLite database?
Storing a timestamp in an SQLite database is done by using the DATETIME
data type. This type is used to store date and time values in the format YYYY-MM-DD HH:MM:SS
. An example of how to store a timestamp in an SQLite database is shown below:
CREATE TABLE IF NOT EXISTS my_table (
id INTEGER PRIMARY KEY,
timestamp DATETIME NOT NULL
);
INSERT INTO my_table (timestamp) VALUES (datetime('now'));
The code above will create a table named my_table
with an id
integer primary key and a timestamp
column of type DATETIME
. The INSERT
statement will add a row to the table with the current timestamp.
Parts of the code:
CREATE TABLE
: This statement creates a new table in the database.IF NOT EXISTS
: This clause ensures that the table is only created if it does not already exist.DATETIME
: This is the data type used to store the timestamp.INSERT INTO
: This statement adds a row to the database table.datetime('now')
: This is a function that returns the current timestamp.
Helpful links
More of Sqlite
- How do I use the SQLite zfill function?
- How can I use an upsert statement to update data in a SQLite database?
- How do I extract the year from a datetime value in SQLite?
- How do I use SQLite with Zephyr?
- How do I use the SQLite YEAR function?
- How to configure SQLite with XAMPP on Windows?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use SQLite on Windows?
- How do I use variables in a SQLite database?
- How do I use UUIDs in SQLite?
See more codes...