Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question seems very similar to <a href="https://stackoverflow.com/questions/537077/python-sqlite3-how-to-convert-a-list-to-a-blob-cell">this earlier SO question</a> so at first I thought that might solve your problem. But looking again at your question it seems like you did read this question since you mention two of the methods they propose. Also, since your datatype is different (list of tuples instead of list of ints) I'm going to give you a pass.</p> <p>Doing some research I find a lot of code samples that use a method <code>sqlite3.Binary()</code> (such as <a href="http://zetcode.com/db/sqlitepythontutorial/" rel="nofollow noreferrer">here</a>). This might be what you want but what worries me is that I can find <strong>absolutely no documentation</strong> for this function in the <a href="http://docs.python.org/library/sqlite3.html" rel="nofollow noreferrer">Sqlite3 Python Interface API</a>. As such, I would advise against using it. I'm guessing this method was deprecated, but I can't find any clear documentation about what replaced it.</p> <p>That said, if you read the <a href="http://docs.python.org/library/sqlite3.html" rel="nofollow noreferrer">Sqlite3 Python Interface API</a>, you see it automatically converts BLOBs to python <a href="http://docs.python.org/library/functions.html#buffer" rel="nofollow noreferrer">buffer</a> objects (and buffer objects to BLOBs). So it seems to me that if you can convert your list to a buffer then you can trivially store it as a BLOB.</p> <p>In my research I found that lists cannot be stored as buffers. I also found that while there are ways of converting a list into a buffer, they require lists of simple types (i.e. not tuples). Therefore, I think your best bet is to define some utility methods for converting your lists to and from strings and then convert the strings to buffers (and back when you retreive them from the database).</p> <pre><code>def myListToStr(myList): """This method takes a list of (int, str) tuples and converts them to a string""" strList = "" for item in myList: num, name = item #split the tuple strList += "{}:{} ".format(num, name) #append the tuple in "num:name" format with a " " delimiter return strList[:-1] #remove the final space (unneeded) def strToMyList(myStr): """This method takes a string in the format "int:str int:str int:str..." and converts it to a list of (int, str) tuples""" myList = [] for tup in myStr.split(" "): #for each converted tuple numStr, name = tup.split(":") #split the tuple num = int(numStr) #NOTE: this will throw an error if numStr.isdigit() is False myList.append(num, name) return myList </code></pre> <p>Now, converting to a buffer is as easy as</p> <pre><code>my_list2Buff = buffer(myListToStr(my_list2)) </code></pre> <p>And back...</p> <pre><code>my_list2 = strToList(str(my_list2Buff)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload