how to compress a python dict to save into mysql
Question: i have a large python dict, maybe 10MB, failed to save it in mysqldb, as
repr(dict) i tried zlib.compress, the result string is ill to save mysql. now
how about compress the dict and convert to binary, and save as blob? any
coding sample?

#update i tried struct with zlib, about 50% reduced for string of 200k char
length.  
test:

    
    
    import zlib, struct, ast
    
    dic={}
    for i in xrange(20):
        dic[str(i)]=i
    s=zlib.compress(repr(dic), 5)
    bs=struct.pack('%ds' %len(s),s)
    s2=struct.unpack('%ds' %len(s),bs)[0]
    s2=zlib.decompress(s2)
    dic2=ast.literal_eval(s2)
    assert dic2==dic
    


Answer: You should be able to save your python dictionary as a BLOB field, but you
must use parametrized queries, [like
this](http://stackoverflow.com/a/1294496/1734130).

If your blob is larger than 1MB, you may have to increase
[`max_allowed_packet`](http://dev.mysql.com/doc/refman/5.1/en/packet-too-
large.html) on both server and client for BLOBs to work.

However, better method would be to insert your dictionary into another table,
one row per key. You may need to create new table or might be able to use some
of the existing tables which should be created just for this purpose.

If that new table is properly indexed, you can take advantage of server being
able to quickly retrieve your data by key.

