Python MySQL insert and retrieve a list in Blob
Question: I'm trying to insert a list of element into a MySQL database (into a Blob
column). This is an example of my code is:

    
    
    myList = [1345,22,3,4,5]
    myListString = str(myList)
    myQuery = 'INSERT INTO table (blobData) VALUES (%s)'
    cursor.execute(query, myListString)
    

Everything works fine and I have my list stored in my database. But, when I
want to retrieve my list, because it's now a string I have no idea how to get
a real integer list instead of a string.

For example, if now i do :

    
    
    myQuery = 'SELECT blobData FROM db.table'
    cursor.execute(myQuery)
    myRetrievedList = cursor.fetch_all()
    print myRetrievedList[0]
    

I ll get :

    
    
    [
    

instead of :

    
    
    1345
    

Is there any way to transform my string [1345,22,3,4,5] into a list ?


Answer: Edit, as per the OP, he (she?) has a list of lists being entered as the blob
field. In that case, the JSON seems a better way to go.

    
    
    import json
    ...
    ...
    myRetrievedList = cursor.fetch_all()
    jsonOfBlob = json.loads(myRetrievedList)
    integerListOfLists = []
    for oneList in jsonOfBlob:
      listOfInts = [int(x) for x in oneList]
      integerListOfLists.append(listOfInts)
    
    return integerListOfLists #or print, or whatever
    

