-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathReadingImage.py
More file actions
65 lines (50 loc) · 1.53 KB
/
ReadingImage.py
File metadata and controls
65 lines (50 loc) · 1.53 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sqlite3
from PIL import Image
# Function for Convert Binary
# Data to Human Readable Format
def convert_data(data, file_name):
# Convert binary format to
# images or files data
with open(file_name, 'wb') as file:
file.write(data)
img = Image.open(file_name)
print(img)
try:
# Using connect method for establishing
# a connection
con = sqlite3.connect('SQLite_Retrieving_data.db')
cursor = con.cursor()
print("Connected Successfully")
# Search from table query
query = "SELECT * FROM Student"
# using cursor object executing our query
cursor.execute(query)
# fectching all records from cursor object
records = cursor.fetchall()
# using for loop retrieving one by one
# rows or data
for row in records:
# storing row[0] in name variable
name = row[0]
# printing name variable
print("Student Name = ", name)
# storing image (currently in binary format)
image = row[1]
# calling above convert_data() for converting
# binary data to human readable
convert_data(image, "D:\Internship Tasks\GFG\sqlite\\" + name + ".png")
print("Yeah!! We have successfully retrieved values from database")
# If we don't have any records in our database,
# then print this
if len(records) == 0:
print("Sorry! Please Insert some data before reading from the database.")
# print exception if found any during program
# is running
except sqlite3.Error as error:
print(format(error))
# using finally, closing the connection
# (con) object
finally:
if con:
con.close()
print("SQLite connection is closed")