在Python中处理PostgreSQL BLOB数据
PostgreSQL是一个开源的对象关系数据库管理系统,它提供多种数据类型来存储数据。BLOB(二进制大对象)数据类型就是一个例子。它用于存储大型二进制数据,例如音频、视频和图像文件。
为了在Python中使用PostgreSQL,我们首先需要安装psycopg2包,它提供了PostgreSQL的Python接口。可以使用pip包管理器进行安装。
语法
pip install psycopg2-binary
安装psycopg2库后,我们需要使用Python连接到我们的PostgreSQL数据库。
处理PostgreSQL中的BLOB数据需要执行以下操作:
创建一个带有BLOB列的表
在PostgreSQL中,应该使用`bytea`数据类型来创建具有BLOB列的表。我们将使用`bytea`数据类型存储最多1GB的二进制数据。
将BLOB数据插入表中
为了将BLOB数据插入表中,我们需要从文件中读取二进制数据并将其插入“image”列。可以使用SQL语句`INSERT`将数据插入到具有BLOB列的表中。
从表中读取BLOB数据
为了读取表中的BLOB数据,我们需要使用`SELECT`语句从“image”列检索二进制数据。
示例
以下是一个示例代码,演示了如何使用psycopg2库在Python中处理PostgreSQL BLOB(二进制大对象)数据。
“id”列的类型为SERIAL,它会自动为每一行生成一个唯一的整数值。“image”列的类型为BYTEA,用于存储BLOB数据。
算法
导入所需的库:psycopg2和io。
使用psycopg2库建立与PostgreSQL数据库的连接。
从连接中创建一个游标对象。
执行一个SELECT语句,从数据库中检索BLOB数据。
使用fetchone()方法检索结果集的第一行。
从结果集中获取BLOB数据并将其存储在一个变量中。
使用io库创建一个BytesIO对象,并将BLOB数据传递给它。
使用BytesIO对象读取数据。
将数据存储在内存中后,您可以对其执行任何必要的处理。
import psycopg2
conn = None
try:
# connect to the PostgreSQL server
'''Establishing Database connection. Fill up your local Database’s user and password.'''
conn = psycopg2.connect(
host='localhost',
dbname='mydb',
user='postgres',
password='user',
port=5432
)
cur = conn.cursor()
# Creating a table with a BLOB column
cur.execute(
"CREATE TABLE blob_datastore (s_no serial, file_name VARCHAR ( 50 ), blob_data bytea)")
# SQL query to insert data into the database.
insert_script = '''
INSERT INTO blob_datastore(s_no,file_name,blob_data) VALUES (%s,%s,%s);
'''
# psycopg2.Binary(File_in_Bytes) is used to convert the binary data.
BLOB_1 = psycopg2.Binary(
open(f"files\toast_flip.mp4", 'rb').read())
BLOB_2 = psycopg2.Binary(
open(f'files\ex.jpg', 'rb').read())
BLOB_3 = psycopg2.Binary(open(f'files\a-gif.gif', 'rb').read())
BLOB_4 = psycopg2.Binary(open(f'files\UNIT IV.pdf'', 'rb').read())
insert_values = [(1, 'toast_flip.mp4', BLOB_1),
(2, 'ex.jpg', BLOB_2),
(3, 'a-gif.gif', BLOB_3),
(4, 'UNIT UV.pdf', BLOB_4)]
for insert_value in insert_values:
cur.execute(insert_script, insert_value)
print(insert_value[0], insert_value[1],
"[Binary Data]", "row Inserted Successfully")
# SQL query to fetch data.
cur.execute('SELECT * FROM BLOB_DataStore')
for row in cur.fetchall():
BLOB = row[2]
open("new"+row[1], 'wb').write(BLOB)
print(row[0], row[1], "BLOB Data is saved")
cur.close()
except(Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.commit()
输出
1 toast_flip.mp4 [Binary Data] row Inserted Successfully 2 ex.jpg [Binary Data] row Inserted Successfully 3 a-gif.gif [Binary Data] row Inserted Successfully 4 UNIT IV.pdf [Binary Data] row Inserted Successfully 1 toast_flip.mp4 BLOB Data is saved in Current Directory 2 ex.jpg BLOB Data is saved in Current Directory 3 a-gif.gif BLOB Data is saved in Current Directory 4 UNIT UV.pdf BLOB Data is saved in Current Directory

结论
借助psycopg2库,开发者可以使用Python轻松地处理PostgreSQL中的BLOB数据。 使用它,用户可以创建带有BLOB列的表,将二进制数据插入到创建的表中,并使用psycop2库从表中检索二进制数据。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP