My program should be able to work in offline-mode, without a connection to the remote MySQL database. For that, I want to create an option to download a specific table to local SQLite database and to reupload it back to the server when the work is done. I try to use the TFDBatchMove component for this task.
The solution works fine for all but one field: MySQL TIMESTAMP. Currently local DB uses INTEGER for storing the timestamp. When I copy data from SQLite to MySQL, i get an error. On the inverse operation timestamp is corrupted.
I suppose I need some type of data mapping, but I haven't found how to set it up. Can someone help?
Error message: "Project VRDB.exe raised exception class EVariantError with message 'Invalid variant type conversion'". "Project VRDB.exe raised exception class EDatabaseError with message 'Invalid value for field 'TimeStamp''".
My test code:
procedure TForm1.ButtonDownloadDBClick(Sender: TObject);
var queryFrom, queryTo: TFDQuery;
FDBatchMove: TFDBatchMove;
begin
queryFrom := TFDQuery.Create(nil);
queryTo := TFDQuery.Create(nil);
FDBatchMove := TFDBatchMove.Create(nil);
with FDBatchMove do begin
// Use "always insert record" mode
Mode := dmAlwaysInsert;
// Erase destination dataset before moving data
Options := [poClearDest];
end;
try
queryFrom.Connection := FDConnection2; //MySQL
queryTo.Connection := FDConnection1; //SQLite
queryFrom.SQL.Clear;
queryFrom.SQL.Add('SELECT * FROM Clients');
queryFrom.Open();
queryFrom.Prepare;
queryFrom.Close;
queryTo.SQL.Clear;
queryTo.SQL.Add('SELECT * FROM Clients');
queryTo.Open();
queryTo.Prepare;
queryTo.Close;
with TFDBatchMoveDataSetReader.Create(FDBatchMove) do begin
DataSet := queryFrom;
Optimise := False;
end;
with TFDBatchMoveDataSetWriter.Create(FDBatchMove) do begin
DataSet := queryTo;
Optimise := False;
end;
FDBatchMove.Execute;
finally
queryFrom.Close;
queryFrom.Destroy;
queryTo.Close;
queryTo.Destroy;
FDBatchMove.Destroy;
end;
end;