-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path45. Binary Large Object (BLOb).py
36 lines (24 loc) · 1.37 KB
/
45. Binary Large Object (BLOb).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sqlite3
# Connect to the database
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
# Create a table to store BLOB data
cursor.execute('''CREATE TABLE images
(id INTEGER PRIMARY KEY, data BLOB)''')
# Open a file and read its binary contents
with open('image.jpg', 'rb') as file:
image_data = file.read()
# Insert the BLOB data into the database
cursor.execute("INSERT INTO images (data) VALUES (?)", (image_data,))
# Commit the transaction
conn.commit()
# Retrieve the BLOB data from the database
cursor.execute("SELECT data FROM images WHERE id = 1")
image_blob = cursor.fetchone()[0]
# Write the BLOB data to a file
with open('image_copy.jpg', 'wb') as file:
file.write(image_blob)
# Close the database connection
conn.close()
# In this example, we create a table to store BLOB data in an SQLite database. We then open an image file in binary mode and read its contents into a variable. We insert the BLOB data into the database using a parameterized SQL statement, and commit the transaction. We then retrieve the BLOB data from the database using a SELECT statement, and write it to a file.
# Note that the process for storing and retrieving BLOBs may vary depending on the database management system being used. The sqlite3 module is used in this example for simplicity, but similar concepts apply to other database management systems as well.