Recent

Author Topic: [SOLVED] Copy a record ?  (Read 2210 times)

TheNick

  • New Member
  • *
  • Posts: 24
  • Pax vobiscum
[SOLVED] Copy a record ?
« on: May 25, 2021, 03:43:38 pm »
Hi all,
I'm trying to make a copy of an existing record into the same dataset with some entries changed.

1) I copy the contents to a XML-file with the SaveToFile-procedure
2) I append a a new record to the dataset
3) I now populate the dataset with the old data by using the LoadFromFile-procedure
4) I post the new record and set the mode to edit
5) I change some data

No matter what I do, I end up with empty data. The code looks something like this:
Code: Pascal  [Select][+][-]
  1. DataModuleRules.SQLSelectRulesList.SaveToFile('SQLSelectRulesList.xml', dfXML);  //This generates a nice XML-file
  2.  
  3. DataModuleRules.SQLSelectRulesList.Append;
  4.  
  5. if FileExists('SQLSelectRulesList.xml') Then
  6.        DataModuleRules.SQLSelectRulesList.LoadFromFile('SQLSelectRulesList.xml'); //The file are being read but nothing happens...
  7.  
  8. DataModuleRules.SQLSelectRulesList.Post;
  9. DataModuleRules.SQLSelectRulesList.Edit;
  10. DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString:='Copy from - '+ DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString;
  11.  
  12.  

Anyone got an idea what is going wrong?

//Cheers Niclas
« Last Edit: July 19, 2021, 08:55:39 pm by TheNick »
Never give up, never surrender! 😃

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Copy a record ?
« Reply #1 on: May 25, 2021, 04:35:26 pm »
Is it just me, or is there an "ApplyUpdate" missing?

EDIT: And shouldn't the LoadFromFile also have a Format specified?
https://www.freepascal.org/docs-html/fcl/bufdataset/tcustombufdataset.loadfromfile.html

EDIT2: Why don't you use "CopyFromDataset"?
https://www.freepascal.org/docs-html/fcl/bufdataset/tcustombufdataset.copyfromdataset.html

EDIT3: another one: Why not use "INSERT INTO SELECT"?
https://www.w3schools.com/sql/sql_insert_into_select.asp
« Last Edit: May 25, 2021, 04:43:37 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

egsuh

  • Hero Member
  • *****
  • Posts: 1291
Re: Copy a record ?
« Reply #2 on: May 26, 2021, 09:15:43 am »
How to copy is your choice depending on your application's conditions, but I would suggest to use SQL statement itself to copy records.  Following is an example from Firebird documentation.

Code: SQL  [Select][+][-]
  1. INSERT INTO Members (NUMBER, name)
  2.   SELECT NUMBER, name FROM NewMembers WHERE Accepted = 1
  3.     UNION
  4.   SELECT NUMBER, name FROM SuspendedMembers WHERE Vindicated = 1

And then you may change the data of copied records.

Of course this is an option.

TheNick

  • New Member
  • *
  • Posts: 24
  • Pax vobiscum
Re: Copy a record ?
« Reply #3 on: May 26, 2021, 11:01:30 am »
Thank you both for your examples and suggestions. From my pov a safe way of doing this (without defining to much stuff around) is by doing a classic record:
Code: Pascal  [Select][+][-]
  1.  
  2. with (rulerecord) do
  3.          begin
  4.               name:='Copy of - ' + DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString;
  5.               FAIClass:= DataModuleRules.SQLSelectRulesList.FieldByName('class').AsString;
  6.               typeofcomp:=DataModuleRules.SQLSelectRulesList.FieldByName('type').AsString;
  7.               created_at:=TimeStamp;
  8.               DataModuleRules.SQLSelectRulesList.Append;
  9.               DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString:=name;
  10.               DataModuleRules.SQLSelectRulesList.FieldByName('created_at').AsLargeInt:=created_at;
  11.               DataModuleRules.SQLSelectRulesList.FieldByName('class').AsString:=FAIclass;
  12.               DataModuleRules.SQLSelectRulesList.FieldByName('type').AsString:=typeofcomp;
  13.          end;
  14.  
   
Can't say that I like it but it works. I was having higher hopes for the 'CopyFromDataSet' but that costed more of effort then to create an record-type (I have probably not figured out yet how to use in a correct way!).

Cheers Niclas
« Last Edit: May 26, 2021, 11:04:06 am by TheNick »
Never give up, never surrender! 😃

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Copy a record ?
« Reply #4 on: May 26, 2021, 11:25:22 am »
If i read it right, your SQLSelectRulesList is already of Type DataSet, (Is it a DataSource/TSQLQuery?)
so it would be something like

DataModuleRules.SQLSelectRulesList.CopyFromDataSet(DataModuleRules.SQLSelectRulesList.DataSet, True);



One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TheNick

  • New Member
  • *
  • Posts: 24
  • Pax vobiscum
Re: Copy a record ?
« Reply #5 on: May 26, 2021, 01:10:08 pm »
If i read it right, your SQLSelectRulesList is already of Type DataSet, (Is it a DataSource/TSQLQuery?)
so it would be something like

DataModuleRules.SQLSelectRulesList.CopyFromDataSet(DataModuleRules.SQLSelectRulesList.DataSet, True);

True, it is an TSQLQuery.

I will try, what you suggested, in my other copydata-procedures.

Thanks,
Niclas
Never give up, never surrender! 😃

 

TinyPortal © 2005-2018