samedi 21 mars 2015

Sqlite auto timestamp - insert then display on DB aware grid

I have some experince with MySql, and am moving to Sqlite for the first time.


The Sqlite documentation for data types, section 1.2 states that



SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values



I would prefer an auto timestamp, but will live with having to pass it in every time if it will get my code working.


Followinf this question, I have declared my field as



`time_stamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,


However, it is not displaying anything on a DB ware grid.


I added an OnDrawCell() and thought that this might help:



var cellText : String;
cellValue : String;
dateTime : TDateTime;
begin
if ARow = 0 then
Exit;

cellValue := myGrid.Cells[ACol, ARow];

case ACol of
0: ; // invisibe column, do nothing
1: cellText := cellValue;
2: begin
dateTime := StrToDateTime(cellValue);
cellText := DateTimeToStr(dateTime);
end;
3: cellText := cellValue;
4: cellText := cellValue;
end;

myGrid.Canvas.FillRect(Rect);
myGrid.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, cellText);


where column 2 is my timestamp, but is apparently empty.


So, my question is, can anyone correct this code snippet, or show me a code example of how to declare an Sqlite column which defaults to the current timestamp and how to display that in a DB aware grid? I am happy enough to store a Unix timestamp, if that would help.


Aucun commentaire:

Enregistrer un commentaire