json - Storing zipped string into sqlite database using python -
I am trying to store a compressed dictionary in my SQL database. First of all, I change the dict in a string using json.dumps , which works fine. Storing this string in DB also works.
In the next step, I am compressing my string using the encoding ("zlib") . . But throws an error in storing the resulting string in my DB
mydict = {"home": "haus", "cat": "katze", "red": u'w \ Xe4yn ', "dictionary": {"1": "asdfhgjl ahsugoh", "2": "s dhgsuoadhu gohsuohgsduohg"}} dbCommand ("creating table testTable (ch1 varchar);") #changed string for jch1 = json .dumps (mydict, sure_ascii = true) print (jch1) # store uncompressed values dbCommand ("insert in testTable (ch1) values ('% s'),"% (jch1)) # compressed json wire cjch1 = jch1.encode ( "Zlib") print (cjch1) # store compressed values dbCommand ("t The first print output: {"home": (% 1) values in the estTable (ch1) values ('% s'); "% (cjch1))
"Haus", "Glossary": {"1": "asdfhgjl ahsugoh", "2": "s dhgsuoadhu gohsuohgsduohg"}, "red": "w \ u00e4yn", "cat": "katze"}
The second print is definitely not readable:
X ワ フ 1 テ PC ㄿ ㅤ YF ネ ノ Õ
Looking forward to any help signal!
Let's approach this from the back: Why are you using GGIP encoding in the first place? Do you think you need to save space in your database? Have you checked how long the strings will be in production? Compression In fact, these stars will need a minimum length before saving storage space (small input can be bigger than output input for string!). If he really saves some disk space: Do you think that the additional CPU load and processing time saved due to GPIP encoding and decoding?
In addition: The result of gzip / zlib compression is a binary blob in Python 2, this type should be of str in Python 3, type byte Should be. In any case, the database needs to know that whatever you are storing is binary data ! For this endeavor, VARCHAR is not the correct data type. After this, there is a quote from MySQL docs:
In addition, if you use binary values from encryption or compression function If you want to store, which may contain arbitrary byte values, then use a Blob column instead to avoid potential problems with a CAR or VARAR column, which changes the space values.
The same idea is correct for other databases Additionally, in the case of SQLite, you should use the BLOB data type (see) to store binary data (if you already have You want to be sure to retract the exact data held: -)).
Comments
Post a Comment