Search code examples
python-3.xsqlitepython-3.6defaultdatetime-format

sqlite3.OperationalError: default DATE value of column is not constant


I am creating a sqlite3 table that accepts records from a server. There should be one date/text column that also has a datetime DEFAULT value, so I can sync a record which times differ from the server's record.

I found a solution on this forum from here. The problem is it gives me the following error on executing the table creation script: sqlite3.OperationalError: default value of column [updated_at] is not constant.

Table is created:

cur.execute('CREATE TABLE IF NOT EXISTS emp_tb(\
                    emp_id INTEGER PRIMARY KEY NOT NULL,\
                    emp_names TEXT NOT NULL,\
                    emp_number TEXT NOT NULL UNIQUE,\
                    ent_id INTEGER NOT NULL,\
                    active INTEGER NOT NULL DEFAULT "0",\
                    updated_at TEXT NULL DEFAULT (datetime("now", "localtime")),\
                    syncstatus INTEGER NOT NULL DEFAULT "0")')

Should I create a trigger? or How can I have a default value in format ("YYYY-MM-DD HH:MM:SS.SSS") in case the update misses a spot?


Solution

  • Use single quotes (') for the datetime options. As mentioned in the comments, they will have to be escaped (because the query is delimited with single quotes).