title: Storing a Pickle in MySql
body: <p>This is something that has been biting me for quite sometime now. I have the following pickle file (named rawFile.raw) that I generated by serializing some Python Dict objects :</p>
  
  <p>content of rawFile.raw (truncated for legibility) :</p>
  
  <pre><code>(dp0
  S'request_body # 1'
  p1
  S''
  p2
  sS'port # 1'
  p3
  I80
  sS'query_params # 1'
  p4
  ccopy_reg
  _reconstructor
  p5
  (cnetlib.odict
  ODict
  p6
  c__builtin__
  object
  p7
  Ntp8
  Rp9
  (dp10
  S'lst'
  p11
  (lp12
  (S'layoutId'
  p13
  S'-1123196643'
  p14
  tp15
  asbsS'headers # 1'
  p16
  g5
  (cnetlib.odict
  ODictCaseless
  p17
  g7
  Ntp18
  Rp19
  (dp20
  g11
  (lp21
  (lp22
  S'sn'
  p23
  aS'2.VI7D9DF640615B4948854C88C5E769B94C.SIE5FB3A28D0DA4F27A3D2C03B8FAAFFAE.VS144257070601359422212.1442570840'
  p24
  aa(lp25
  S'Browser-Name'
  p26
  aS'Mobile Safari'
  p27
  aa(lp28
  S'Accept-Encoding'
  p29
  aS'gzip'
  p30
  aa(lp31
  S'secureToken'
  p32
  aS'5nANXZrwYBrl9sNykA+qlpLsjHXlnF97tQLHnPgcjwZm9u0t8XAHtO4XTjKODcIb0ee4LlFchmUiptWZEPDUng=='
  p33
  aa(lp34
  S'User-Agent'
  p35
  aS'Mozilla/5.0 (Linux; U; Android 4.3; en-us; Google Galaxy Nexus - 4.3 - API 18 - 720x1280 Build/JLS36G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 FKUA/Retail/590206/Android/Mobile (Genymotion/Google Galaxy Nexus - 4.3 - API 18 - 720x1280/fd6babaa1ff9127ed7e9cc7a916639e5)'
  p36
  aa(lp37
  S'Device-Id'
  p38
  aS'fd6babaa1ff9127ed7e9cc7a916639e5'
  p39
  aa(lp40
  S'Host'
  p41
  aS'mobileapi.flipkart.net'
  p42
  aa(lp43
  S'Connection'
  p44
  aS'Keep-Alive'
  p45
  aasbsS'method # 1'
  p46
  S'GET'
  p47
  sS'scheme # 1'
  p48
  S'http'
  p49
  sS'path # 1'
  p50
  (lp51
  S'3'
  p52
  aS'layout'
  p53
  aS'widget'
  p54
  aS'productBundle'
  p55
  as.(dp0
  S'request_body # 1'
  p1
  S''
  p2
  sS'method # 1'
  p3
  S'GET'
  p4
  sS'query_params # 2'
  p5
  ccopy_reg
  _reconstructor
  p6
  (cnetlib.odict
  ODict
  p7
  c__builtin__
  object
  p8
  Ntp9
  Rp10
  (dp11
  S'lst'
  p12
  (lp13
  sbsS'query_params # 1'
  p14
  </code></pre>
  
  <p>... more similar content truncated.</p>
  
  <p>Now I need to store the contents of this file in a column in a MySql table. Through some Googling I learned that these should be stored in a BLOB column. So I did. Also if you observe, the above content has a lot of single and (may have) double quotes as well. I am using MySqlDB python connector. So when I try inserting the above data in the respective column as :</p>
  
  <pre><code>INSERT INTO session_logs(request_as_on, session_pickle) VALUES (%s, %s)
  </code></pre>
  
  <p>and then </p>
  
  <pre><code>self.cur.execute(insertValueQry,(parser.parse(reqTimeStamp).strftime('%Y-%m-%d %H:%M:%S'), putThisPickleInDb))
  </code></pre>
  
  <p>I get the following error :</p>
  
  <blockquote>
    <p>mysqldb insertion error while inserting Error 1241: Operand should contain 1 column(s)</p>
  </blockquote>
  
  <p>So to solve the above I replace the above execute statement with :</p>
  
  <pre><code>self.cur.execute(insertValueQry,(parser.parse(reqTimeStamp).strftime('%Y-%m-%d %H:%M:%S'), self.con.escape_string(str(putThisPickleInDb))))
  </code></pre>
  
  <p>and it solves the problem by escaping all single and double quotes from the pickle data. But it does not store the data in the table in the same format as it was in the raw file.</p>
  
  <p>Apparently hence, now when I need to read back this pickle content from the db, </p>
  
  <ol>
  <li><p>it is still escaped - So I do the following to un-escape it and get the original content:</p>
  
  <pre><code>aalu = "" &lt;br&gt;
  aalu = aalu + str(userSelectedSessionPickle[0]).decode('string_escape').decode('string_escape')
  </code></pre></li>
  </ol>
  
  <p>where userSelectedSessionPickle is a tuple returned from the db.</p>
  
  <ol start="2">
  <li><p>After un-escaping it I try making a pickle out of it again with:</p>
  
  <pre><code>pickle.dump (aalu, currentPickleRawFile)
  </code></pre></li>
  </ol>
  
  <p>so that I can load the pickle file later and use it as needed. But when doing a pickle.load on the file currentPickleRawFile, it throws the following error :</p>
  
  <blockquote>
    <p>ValueError: dictionary update sequence element #0 has length 1; 2 is required</p>
  </blockquote>
  
  <p>Just in case if it helps : When I open the file currentPickleRawFile in a text editor, the format of the contents in it is different than in rawFile.raw (content shown above).</p>
  
  <p><strong>All I need is store the above pickle file in a db and then be able to load it back.</strong> How can I get this done? </p>