Python/SQLite将列表存储为二进制文件(blob) [英] Python/SQLite storing lists as binaries (blobs)

查看:579
本文介绍了Python/SQLite将列表存储为二进制文件(blob)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlite的官方文档建议将列表存储为二进制对象. Google引导我提出了各种建议.一种是使用数组模块(array.array('B',my_list2),但这不适用于非平凡的列表:

The official documentation for sqlite suggests storing lists as binary objects. Google led me to various suggestions. One was to use the array module (array.array('B', my_list2), but this didn't work for a non-trivial list:

my_list2 = [(23,"Bob"), (22,"Alice")]
array.array('B',my_list2)

TypeError: an integer is required

另一个建议涉及使用泡菜,但是有人插话声称它不安全.最后的建议是为每个列表变量创建一个新表,其中有几个.不过,我还是很犹豫要制定一个复杂的架构.

Another suggestion involved using pickle, but someone interjected claiming it was not secure. One final suggestion is creating a new table for each list variable, of which there are a few. I'm hesitant to make a complex schema though.

我该怎么办?我应该如何将my_list2以及其他列表存储在数据库中?

What should I do? How should I store my_list2, along with the other lists, in a database?

修改

找到了一种优雅整洁的解决方案,该解决方案只需最少的代码即可处理简单和复杂的案件:

Found an elegant and tidy solution that works for simple and complex cases with minimal code:

import json
my_list2 = [(23,"Bob Baker"), (22,"Alice Adams")]
my_list2_str = json.dumps(my_list2)
print type(my_list2_str)
 <type 'str'>
list2 = json.loads(my_list2_str)
print list2, type(list2)
 [(23, u"Bob Baker"), (22, u"Alice Adams")] <type 'list'>

推荐答案

这个问题似乎与

This question seems very similar to this earlier SO question 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.

做一些研究,我发现很多使用方法sqlite3.Binary()的代码示例(例如此处).这可能就是您想要的,但令我担心的是,我在完全没有文档 "rel =" nofollow noreferrer> Sqlite3 Python接口API .因此,我建议不要使用它.我猜想此方法已被弃用,但找不到任何替代它的清晰文档.

Doing some research I find a lot of code samples that use a method sqlite3.Binary() (such as here). This might be what you want but what worries me is that I can find absolutely no documentation for this function in the Sqlite3 Python Interface API. 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.

也就是说,如果您阅读 Sqlite3 Python接口API ,则会看到它自动将BLOB转换为python buffer 对象(以及将缓冲区对象转换为BLOB).因此在我看来,如果可以将列表转换为缓冲区,则可以将其简单地存储为BLOB.

That said, if you read the Sqlite3 Python Interface API, you see it automatically converts BLOBs to python buffer 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.

在我的研究中,我发现列表不能存储为缓冲区.我还发现,虽然有将列表转换为缓冲区的方法,但它们需要简单类型的列表(即不是元组).因此,我认为最好的选择是定义一些实用程序方法,用于将列表与字符串进行相互转换,然后将字符串转换为缓冲区(从数据库检索字符串时又返回缓冲区).

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).

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

现在,转换为缓冲区就像

Now, converting to a buffer is as easy as

my_list2Buff = buffer(myListToStr(my_list2))

然后回来...

my_list2 = strToList(str(my_list2Buff))

这篇关于Python/SQLite将列表存储为二进制文件(blob)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆