【问题标题】:Saving Images into SQLite database将图像保存到 SQLite 数据库
【发布时间】:2020-02-10 10:26:51
【问题描述】:

我有一个从网站收集一些数据的程序。文本数据附加到“信息”数据帧中,照片网址附加到“照片”数据帧中。

我已经在我的 SQL 数据库中插入了“info”表并且工作得很好!

data.to_sql('Flat', con=conn, if_exists='replace',index=False)

现在我需要了解如何将图像链接转换为 Blob 数据并将其插入数据库。

【问题讨论】:

  • 在这些链接下载图像内容,然后将其存储为 BLOB。轻松愉快。

标签: python sqlite blob


【解决方案1】:

BLOB 是 Binary Large OBjects。首先您需要将图像转换为二进制对象。

def convertToBinaryData(imageLocation):
    #Convert digital data to binary format
    with open(imageLocation, 'rb') as file:
        blobData = file.read()
    return blobData

其余的是简单的插入,请确保您已连接。创建一个插入语句,将您的二进制文件注入到该语句中。

 insert = """ INSERT INTO 'images' ('id', 'image') VALUES (?, ?) """
 id = 1
 image = convertToBinary(imageLocation)
 cursor.execute(insert, (id, image))
 connection.commit()

这些函数省略了如何创建连接和获取游标,但是可以在以下位置找到完整示例:https://pynative.com/python-sqlite-blob-insert-and-retrieve-digital-data/

【讨论】:

  • 很好,但我有图片网址)
  • 根据图像的大小,您可以请求它们,然后将它们转换为 blob。或者甚至将它们请求为 blob。 stackoverflow.com/questions/34077942/…
  • 我怎样才能将它们作为 Blob 获得?
  • 取决于提供者或工具我留下了另一个问题的链接,该问题包含准确的代码。
猜你喜欢
  • 2012-12-16
  • 2020-11-30
  • 2011-06-29
  • 2012-01-18
  • 2012-06-04
  • 1970-01-01
相关资源
最近更新 更多