How to serialize a dataset?

Well, I had just figured out how to do it in pure Python by converting the dataset to a list of lists, using the python pickle module to serialize it, base64 encoding it and putting it in the database… but thanks to pturmel I was able to get the Java version working in just a couple minutes. I also referred to this blog post: joeygibson.com/2003/05/22/jytho … oo-useful/ . So here’s how I got it to work.

Database (PSQL) has some character varying fields for identifying the save, and a bytea field for the actual data.

In the save script, use the bytearrayoutputstream and objectoutputstream to save the Tag Pens dataset (will be extending to save all the runtime configurable datasets):

[code]import java.io

baos = java.io.ByteArrayOutputStream()
oos = java.io.ObjectOutputStream(baos)
oos.writeObject(event.source.parent.getComponent(‘Easy Chart’).tagPens)
oos.close()
bytesToWrite = baos.toByteArray()
system.db.runPrepUpdate(
‘’‘INSERT INTO settings (project, system, username, bin_tag_pens)
VALUES (‘projectname’, ‘systemname’, ?, ?);’’’,
[system.tag.read("[System]Client/User/Username").value,bytesToWrite],
‘trendsettings’)[/code]

To retrieve the data:

[code]queriedData = system.db.runScalarPrepQuery(
‘’‘SELECT bin_tag_pens FROM settings
WHERE
project = ‘projectname’ AND
system = ‘systemname’
AND username = ?;’’’,
[system.tag.read("[System]Client/User/Username").value],
‘trendsettings’)

if queriedData != None:
import java.io
bais = java.io.ByteArrayInputStream(queriedData)
ois = java.io.ObjectInputStream(bais)
retrievedData = ois.readObject()
event.source.parent.getComponent(‘Easy Chart’).tagPens = retrievedData
else:
system.gui.messageBox(‘No results’)

[/code]

Another good thing about this is that I can just write all 5 datasets for the EasyChart directly into the same BAOS / OOS object and as long as I retrieve them in order, I won’t need separate columns for each.

Thanks pturmel! That’s a lot easier than the pickle method…

1 Like