使用 PYODBC 将 BitTorrent 位字段插入 MSSQL 中的 VarBinary(MAX)
我正在开发一个涉及 BitTorrent 的项目,在该项目中我收到一个 python 字符串形式的位字段。例如:
bitfield = "000001110100111000110101100010"
我希望能够将 python 字符串转换为一种格式,以便可以使用 PYODBC 将其按原样插入到 MSSQL 数据库的 varbinary(max) 列中。如果我尝试将其作为字符串插入,它当然会抱怨非法转换错误。
请注意,根据其文档,PYODBC 需要字节数组或缓冲区作为 varbinary 字段的输入。
任何建议将不胜感激。
I'm working on a project involving BitTorrent, where I receive an bitfield as a python string. For example:
bitfield = "000001110100111000110101100010"
I would like to be able to convert the python string into a format so it can be inserted as is into a varbinary(max) column of a MSSQL database using PYODBC. If I try to insert it as a string as is, it of course complains of an illegal conversion error.
Note PYODBC, expects a byte array or buffer as input for a varbinary field according to their documentation.
Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是最新版本的 python,您可以利用标准库
struct
模块和bin
函数。下面是一个简单的示例:最终语句的结果是
哪个是初始位域(删除了前导零)。
您可以在 docs.python.org 上找到 struct 模块的文档。 bin 函数的文档也可以在同一个地方找到。
Assuming you're using a recent version of python you can take advantage of the standard library
struct
module and thebin
function. Here's a quick example:The result of the final statement is
which is the initial bitfield (with the leading zeroes removed).
You can find the documentation for the struct module on docs.python.org. The documentation for the bin function is also available at the same place.
在开始编码之前,我想提出一个建议:“位字段”值不是可以分为字节的长度。我建议,每当您处理位字符串时,都以字节大小来增加它们(例如 if len(bitfield)%8 != 0: print 'Make make certain the bitfield can be full returned by bytes!')确保在不同的编程语言、编程语言中的不同库以及不同的数据库中操作字段的方式没有歧义。换句话说,数据库、Python、我要推荐的库等都将存储或能够以字节数组的形式表示这个位数组。如果提供的位数组没有均匀地划分为字节,则会发生以下三种情况之一:
1)会引发错误,(这是乐观的)
2) 位数组将自动神奇地左填充。
3) 位数组将自动神奇地右填充。
我建议使用某种位串库。为此,我使用了 python-bitstring 。我没有花时间在这里处理 ODBC,但想法基本相同,并利用 srgerg 的答案:
示例:
其输出为:
请注意,因为您没有可以直接分解的位串转换为字节(它是代表 30 位的字符串),获得完全相同字符串的唯一方法是指定长度,即使这样,结果也不一致,具体取决于 BitArray 的实例化方式。
Before I get to code, I'd like to make one recommendation: The 'bitfield' value isn't a length that can be divided into bytes. I'd suggest that anytime you're dealing with bit strings, you grow them in sizes of bytes (e.g. if len(bitfield)%8 != 0: print 'Make sure the bitfield can be fully represented by bytes!') to ensure there is no ambiguity in how fields are manipulated in different programming languages, different libraries within programming languages, and different databases. In other words, the database, python, the library I'm going to recommend, etc. are all going to either store or be able to represent this bitarray in the form of a byte array. If the bitarray provided doesn't divide evenly into bytes, one of three things will happen:
1) An error will be raised, (this is optimistic)
2) The bitarray will be auto-magically left padded.
3) The bitarray will be auto-magically right padded.
I'd recommend using a bitstring library of some sort. I have used python-bitstring for this purpose. I didn't take the time to deal with ODBC here, but the idea is basically the same, and leverages srgerg's answer:
Examples:
The output of this is:
Note that since you don't have a bitstring that can be directly broken down into bytes (it's a string that represents 30 bits), the only way to get the exact same string was to specify a length, and even then the results were not consistent depending on how the BitArray was instantiated.