Einzelnen Beitrag anzeigen

wschrabi

Registriert seit: 16. Jan 2005
448 Beiträge
 
#1

SQLLite mit INTEGER für DATETIME

  Alt 9. Sep 2014, 13:06
Datenbank: SQLLite • Version: letzte • Zugriff über: FireMonkey
Hallo,
ich knoble jetzt schon den ganzen Tag.
Ich möchte ineinem SQLite Feld das Messdatum als INTEGER (= UNIX TIME STAMP) speichern.
Doch in dem ListView soll es HUMAN READABLE Sein. Also hab ich mir gedacht, mit OnCalcFields arbeiten.
Doch es klappt einfach nicht.
Was mache ich da falsch? Es soll unter XE6 auf Android laufen.
Delphi-Quellcode:
unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
  FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async,
  FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Stan.ExprFuncs,
  FireDAC.FMXUI.Login, FireDAC.Comp.UI, Data.DB, FireDAC.Comp.Client,
  FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt,
  FMX.ListView.Types, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Data.Bind.Controls,
  System.Rtti, System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.Components,
  FMX.Layouts, Fmx.Bind.Navigator, Data.Bind.DBScope, FMX.ListView,
  FireDAC.Comp.DataSet, FMX.StdCtrls, FireDAC.FMXUI.Wait, System.IOUtils, DateUtils;

const
  cnFormat = 'yyyy-mm-dd';
    
type
  TForm1 = class(TForm)
    FDConnection1: TFDConnection;
    FDGUIxLoginDialog1: TFDGUIxLoginDialog;
    FDQueryCreateTable: TFDQuery;
    ListView1: TListView;
    BindSourceDB1: TBindSourceDB;
    BindingsList1: TBindingsList;
    FDQuery1: TFDQuery;
    BindSourceDB2: TBindSourceDB;
    FDQuery2: TFDQuery;
    NavigatorBindSourceDB2: TBindNavigator;
    ButtonDelete: TButton;
    FDQueryIns: TFDQuery;
    ButtonAdd: TButton;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    FDQueryDel: TFDQuery;
    Label1: TLabel;
    LinkFillControlToFieldMessdatum: TLinkFillControlToField;
    FDQueryDrop: TFDQuery;
    Label2: TLabel;
    procedure ListView1ItemClick(const Sender: TObject;
      const AItem: TListViewItem);
    procedure ButtonAddClick(Sender: TObject);
    procedure FDConnection1BeforeConnect(Sender: TObject);
    procedure ButtonDeleteClick(Sender: TObject);
    procedure ButtonDropClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FDQuery2CalcFields(DataSet: TDataSet);
    
  private
    { Private-Deklarationen }
    FmtStngs: TFormatSettings;
    
    
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}


procedure TForm1.ButtonAddClick(Sender: TObject);
var
  TaskName: String;
begin
  try
    TaskName:=DateToStr(now()(*, FmtStngs*));
    if InputQuery('Bitte neues Meßdatum eingeben', 'Messdatum', TaskName) and (TaskName.Trim <> '') then
    begin
      ShowMessage('UNIX Timestamp:'+Inttostr( DateTimeToUnix(StrToDateTime(TaskName))));
      //FDQueryIns.ParamByName('MessDatum').AsDate := StrToDate(TaskName(*, FmtStngs*));
      FDQueryIns.ParamByName('MessDatum').AsInteger := DateTimeToUnix(StrToDateTime(TaskName));
      FDQueryIns.ExecSQL();
      FDQuery2.Close;
      FDQuery2.Open;
      Label2.Text:= 'Total:' + IntTOStr(FDQuery2.RowsAffected);
      
      ButtonDelete.Visible := ListView1.Selected <> nil;
    end;
  except
    on e: Exception do
    begin
      ShowMessage(e.Message);
    end;
  end;
end;

procedure TForm1.ButtonDeleteClick(Sender: TObject);
var
  TaskName: String;
begin
  TaskName := ListView1.Selected.Text;

  try
    FDQueryDel.ParamByName('MessDatum').AsInteger := DateTimeToUnix(StrToDateTime(TaskName));
    FDQueryDel.ExecSQL();
    FDQuery2.Close;
    FDQuery2.Open;
    Label2.Text:= 'Total:' + IntTOStr(FDQuery2.RowsAffected);
    ButtonDelete.Visible := ListView1.Selected <> nil;
  except
    on e: Exception do
    begin
      SHowMessage(e.Message);
    end;
  end;
end;

procedure TForm1.ButtonDropClick(Sender: TObject);
begin
   FDQueryDrop.ExecSQL();
end;

procedure TForm1.FDConnection1BeforeConnect(Sender: TObject);
begin
  {$IF DEFINED(iOS) or DEFINED(ANDROID)}
  FDConnection1.Params.Values['Database'] :=
      TPath.Combine(TPath.GetDocumentsPath, 'lipoapp.s3db');
  {$ENDIF}
end;

procedure TForm1.FDQuery2CalcFields(DataSet: TDataSet);
begin
   DataSet.FieldByName('Messdatum').AsDateTime := UnixToDateTime(DataSet.FieldByName('Messdatum').AsInteger);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    FmtStngs:=TFormatsettings.Create;
    FmtStngs.DateSeparator := '.';
    FmtStngs.ShortDateFormat := 'yyyy.mm.dd';
    FmtStngs.TimeSeparator := ':';
    FmtStngs.LongTimeFormat := 'hh:mm:ss';
  //fSetting := GetCustomDateFormatSettings;
end;

procedure TForm1.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
begin
   ButtonDelete.Visible:=ListView1.Selected <> nil;
end;

end.
  Mit Zitat antworten Zitat