[Newbies] Exporting Object Memory
C. David Shaffer
cdshaffer at acm.org
Mon Dec 17 16:33:44 UTC 2007
Chris Cunnington wrote:
> If I had data in object memory, because I've saved answers from a user into
> objects, then can I file out that information? If I had to send it to
> somebody as an csv file, how could I do that easily?
> I know if I could find an object with the inspector I could print data in
> the Transcript and cut and paste it into another file, but is there another
> It seems to me that filing out gives you the application, but what if I
> wanted information that had been entered by the users, the end user data?
The end-user data is presumable stored in your application's object
model. Normally for me that is some kind of "database" class. Let's
call it "Database" and assume it understands #default (which returns the
only Database instance).
There are lots of ways to answer your question so let me mention a
couple in increasing order of work involved:
You could export your data as XML using SIXX (load from SqueakMap).
You could export your data in binary format using ImageSegments
You could write your data to CSV
Using SIXX you would just do this:
s := SixxWriteStream newFileNamed: 'myData.sixx'.
s nextPut: Database default.
That's it. You can read your data back in using a SixxReadStream.
Now, while SIXX is the easiest that I can think of you mentioned CSV so
I'll assume you want it in that format for some reason. In any case, if
XML is good enough just write your data to a file with:
Let's further suppose that the database instance understands #data which
answers a collection of data items that all understand #printCSVOn:.
Then I'd write that database to a file with:
fileStream := FileDirectory current newFileNamed: 'someFile.csv'.
[Database default data do: [:dataItem |
dataItem printCSVOn: fileStream.
ensure: [fileStream close].
Notice that I make sure that the fileStream get's closed even if there
is an error during the writing. Now, of course, the real work is in
printCSVOn: which obviously depends on your data. Suppose you have an
Account class which has i-vars for accountNumber, ownerName,
ownerPhoneNumber. You could write them out with:
aStream nextPutAll: '"' , accountNumber , '",'.
aStream nextPutAll: '"' , ownerName ,'",'.
aStream nextPutAll: '"' , ownerPhoneNumber ,'"'.
Of course CSV will probably be useless if your database has more than
one type of thing in it (each line will have different columns). You
could modify this example so that each different kind of thing stored in
your database is written to a different file.
More information about the Beginners