Forum > Databases

[SOLVED] Copy a record ?

(1/2) > >>

TheNick:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---DataModuleRules.SQLSelectRulesList.SaveToFile('SQLSelectRulesList.xml', dfXML);  //This generates a nice XML-file DataModuleRules.SQLSelectRulesList.Append; if FileExists('SQLSelectRulesList.xml') Then       DataModuleRules.SQLSelectRulesList.LoadFromFile('SQLSelectRulesList.xml'); //The file are being read but nothing happens... DataModuleRules.SQLSelectRulesList.Post;DataModuleRules.SQLSelectRulesList.Edit;DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString:='Copy from - '+ DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString;  
Anyone got an idea what is going wrong?

//Cheers Niclas

Zvoni:
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

egsuh:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---INSERT INTO Members (NUMBER, name)  SELECT NUMBER, name FROM NewMembers WHERE Accepted = 1    UNION  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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- with (rulerecord) do         begin              name:='Copy of - ' + DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString;              FAIClass:= DataModuleRules.SQLSelectRulesList.FieldByName('class').AsString;              typeofcomp:=DataModuleRules.SQLSelectRulesList.FieldByName('type').AsString;              created_at:=TimeStamp;              DataModuleRules.SQLSelectRulesList.Append;              DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString:=name;              DataModuleRules.SQLSelectRulesList.FieldByName('created_at').AsLargeInt:=created_at;              DataModuleRules.SQLSelectRulesList.FieldByName('class').AsString:=FAIclass;              DataModuleRules.SQLSelectRulesList.FieldByName('type').AsString:=typeofcomp;         end;    
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

Zvoni:
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);



Navigation

[0] Message Index

[#] Next page

Go to full version