tags:

views:

241

answers:

3

Hi,

I need to insert a python tuple (of floats) into a MySQL database. In principle I could pickle it and insert it as a string, but that would grant me the chance only to retrieve it through python. Alternative is to serialize the tuple to XML and store the XML string.

What solutions do you think would be also possible, with an eye toward storing other stuff (e.g. a list, or an object). Recovering it from other languages is a plus.

+2  A: 

How about JSON ... it's compact, and there are interpreters available for it in most languages.

Sean Vieira
+3  A: 

Make another table and do one-to-many. Don't try to cram a programming language feature into a database as-is if you can avoid it.

If you absolutely need to be able to store an object down the line, your options are a bit more limited. YAML is probably the best balance of human-readable and program-readable, and it has some syntax for specifying classes you might be able to use.

Eevee
yes you have a point, however. Supppose I have to express an xyz coordinate for a point, and I have one row for each point. you would have two choices: either have three different columns, for x y and z, or you store the tupòle as a single entity in a column. In this case, things are easy because you know how many elements make your tuple (three) but in my case, I don't know a priori. I could do a one to many and insert each element of the tuple, you are right, but I fear that this would just move the problem (e.g. suppose I have a tuple of tuples)
Stefano Borini
That's different, then, although an odd case. Sticking with the YAML recommendation, then. Easy to read and write, has bindings to a C library in most languages, respects data types, and supports arbitrary user-defined class names.
Eevee
+2  A: 

I'd look at serializing it to JSON, using the simplejson package, or the built-in json package in python 2.6.

It's simple to use in python, importable by practically every other language, and you don't have to make all of the "what tag should I use? what attributes should this have?" decisions that you might in XML.

Ian Clelland
The problem with JSON is that it collapses everything to a string. If I have a tuple of integers, I'd like to retrieve it as a tuple of integers, not as a tuple of strings. In javascript this does not make much of a difference, since it's loose typed, but in python it's a different story. So no, JSON is not an option in this case.
Stefano Borini
JSON doesn't require everything to be a string. It uses JavaScript-native types: string, int, float, boolean, null.
Ned Batchelder
oh, this changes things then.
Stefano Borini