Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a date column to a table with current date as default value?

Tags:

sqlite

Where I can see which default columns SQLite table will have after creating? I need a column with datetime which has default value of time when data added to the table.\

Now I'm trying:

ALTER TABLE recipes ADD COLUMN timestamp DATE DEFAULT (datetime('now','localtime'));

And I'm obtaining the following message:

SQLiteManager: Likely SQL syntax error: ALTER TABLE recipes ADD COLUMN timestamp DATE DEFAULT (datetime('now','localtime')); [ Cannot add a column with non-constant default ]
Exception Name: NS_ERROR_FAILURE
 Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)         [mozIStorageConnection.createStatement]
like image 644
Stas Avatar asked Jul 24 '12 12:07

Stas


People also ask

How do I add a date column to an existing table in SQL?

ADD DateOfBirth date; Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type specifies what type of data the column can hold. For a complete reference of all the data types available in MS Access, MySQL, and SQL Server, go to our complete Data Types reference.

How do I insert default date?

On the Insert tab, in the Text group, click Date & Time. In the Date and time dialog box, select the format you want. Select the Update automatically check box. The date is inserted as a field and will update automatically.


1 Answers

Use the create table instead of altering the table. If there are values inside you'll have this kind of a problem. See

http://sqlite.1065341.n5.nabble.com/Can-t-insert-timestamp-field-with-value-CURRENT-TIME-td42729.html

all the best

like image 193
devanand Avatar answered Oct 27 '22 14:10

devanand