Sqlite setting the default value in create table

浪尽此生 提交于 2020-01-03 17:22:22

问题


I wrote something like

create table if not exists QuickTest (
id integer primary key NOT NULL,
a TEXT DEFAULT @0,
b TEXT,
c TEXT);

I get an error on @0. Is there a way to insert parameters here or do i need to hardcode the value in? I typically like using parameters when setting values.


回答1:


You have to use string constant or NULL.

http://www.sqlite.org/syntaxdiagrams.html#column-constraint

The DEFAULT constraint specifies a default value to use when doing an INSERT. The value may be NULL, a string constant, a number, or a constant expression enclosed in parentheses. The default value may also be one of the special case-independant keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. If the value is NULL, a string constant or number, it is inserted into the column whenever an INSERT statement that does not specify a value for the column is executed. If the value is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the current UTC date and/or time is inserted into the columns. For CURRENT_TIME, the format is HH:MM:SS. For CURRENT_DATE, YYYY-MM-DD. The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".




回答2:


You can't use parameters in a ddl (data definition language) statement, only in dml (data manipulation language) statement and select statements are parameters allowed.




回答3:


CREATE TABLE test (id INTEGER DEFAULT -1, name VARCHAR DEFAULT na)

Above sql will create a table test with two colums id and name While executing insert for this table if no values are specified for id and name then it will insert -1 for id column and na will be inserted for name column



来源:https://stackoverflow.com/questions/2059266/sqlite-setting-the-default-value-in-create-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!