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 generate a row number for each record in a SQLite database?
- How do I use SQLite with Zephyr?
- How do I create a database using SQLite?
- How do I download and install SQLite zip?
- How do I use SQLite to retrieve data from a specific year?
- How can SQLite and ZFS be used together for software development?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I import data from a SQLite zip file?
- How do I extract the year from a datetime value in SQLite?
- How do I use an SQLite UPDATE statement with a SELECT query?
See more codes...