tags:

views:

336

answers:

3

In SQLite database how does one define a column default to be the current user?

+2  A: 

Are you talking about the default value for the column? The only valid default values for SQLite are as follows:

The DEFAULT constraint specifies a default value to use when doing an INSERT. The value may be NULL, a string constant or a number. The default value may also be one of the special case-independant keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.

Kyle Cronin
actually not *entirely* true; I have a column declared as follows: "timestamp" TEXT DEFAULT (strftime('%Y-%m-%dT%H:%M','now', 'localtime')), which has a function value as default. Maybe this can be categorized as string constant?
polyglot
+2  A: 

SQLite does not have the concept of a user. You don't "log in" with a username/password when you connect to SQLite.

If you need the current operating system user, you should specify this as a string when you insert data from your application.

Bill Karwin
+1  A: 

Simply put: one doesn't. Intending to do so would mean your database has to be aware of the application state, which is kind of backwards.

Aram Verstegen
I'm not sure of that. In most big businesses their internal audit departments demand extra columns in ever tables with details like last_edited_by, last_edited_at. I always make these columns default to current user and current datetime. Doing it any other way would just be plain hard work.
jussij