Storing data in an external file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Storing data in an external file

Post by MaxV » Tue Jun 04, 2013 3:13 pm

Hello,
I read here and there about storing data, and I need to store just 4 data in an external file. I think that the best way is to save all properties of an object.
Is there any way to save an object?
An object has custom properties, if I could save an object, I could load it and all my data would be perfectly parsed for retrieving data.
Lets see the following example:

Code: Select all

set the owner of field "car" to "Max"
set the model of field "car" to "Ferrari"
set the age of field "car" to  2
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Storing data in an external file

Post by Klaus » Tue Jun 04, 2013 3:57 pm

Hi Max,

no way to store a single object in an external file, sorry!
Hint: "owner" is a reserved word in Livecode, so you shopuld change the name for that property!

Best

Klaus

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Storing data in an external file

Post by MaxV » Tue Jun 04, 2013 4:36 pm

I thought also about array:

Code: Select all

put "Max" into  car["owner"]
put "Ferrari" into  car["model"]
put 2 into car["age"]
but how can I save an array in a file and retrieve array data from a file?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Storing data in an external file

Post by jmburnod » Tue Jun 04, 2013 4:37 pm

Hi MaxV,
You can get all properties of an object, export it in a file and import it to create this object.
No tested but should work.

Code: Select all

on mouseUp
   put the short id of the target into tID
   put GetPropControl(tID)  into fld "fProp"
end mouseUp

function GetPropControl pControlID 
   put empty into tPropControl
   put the properties of control id pControlID into tArray
   put the keys of tArray into tKeys
   repeat for each line tProp in tKeys
      put tProp & tab & tarray[tProp] & cr after tPropControl
   end repeat
   delete char -1 of tPropControl
   return tPropControl
end GetPropControl
And you can do the same for the customproperties

Best regards
Jean-Marc
https://alternatic.ch

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Storing data in an external file

Post by MaxV » Tue Jun 04, 2013 4:52 pm

Wow, I found a solution here: http://lessons.runrev.com/s/lessons/m/4 ... e-it-again
I can use arrayEncode and arrayDecode!!! :-)
Last edited by MaxV on Wed Jun 05, 2013 9:13 am, edited 1 time in total.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Storing data in an external file

Post by FourthWorld » Tue Jun 04, 2013 6:44 pm

You could use a stack file for that, setting the filename property and then using the save command.

But perhaps just as simple would be to work with an array while in memory, saving it to disk with arrayEncode, to read later after passing it through arrayDecode.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Storing data in an external file

Post by Mark » Wed Jun 05, 2013 11:04 am

Jean-Marc, the properties property doesn't include all properties of an object. Therefore, I'm not entirely sure that it can be used to recreate an object entirely. Sometimes it works, sometimes it doesn't, depending on which properties you have changed and which object you want to save.

MaxV, that's a great find. ArrayEncode and ArrayDecode may do what you need. Here's a simple and short script, which does what you want:

Code: Select all

on mouseUp
   put the long id of fld "Car" into myField
   put "C:\folder\file.enc" into myFilePath
   put arrayEncode(the customproperties of myField) into myEncArray
   put myEncArray into url ("binfile:" & myFilePath)
end mouseUp

on mouseUp
   put the long id of fld "Car" into myField
   put "C:\folder\file.enc" into myFilePath
   put arrayDecode(url ("binfile:" & myFilePath)) into myArray
   set the customproperties of myField to myArray
end mouseUp
You'll have to change the file path in each handler.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply